From 65baba0b51e850c7d54a8a970cf4c423226cafc9 Mon Sep 17 00:00:00 2001 From: Andrew Roslik Date: Wed, 2 Nov 2016 18:42:39 +0200 Subject: [PATCH] Implemented #128: Disable validation for heredoc and nowdoc in PHP code. - Added cutting heredoc and nowdoc in PHP files. --- src/config/filters/HereNowdoc.xml | 20 ++++++++ src/lib/PreCommit/Filter/HereNowdoc.php | 66 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/config/filters/HereNowdoc.xml create mode 100644 src/lib/PreCommit/Filter/HereNowdoc.php diff --git a/src/config/filters/HereNowdoc.xml b/src/config/filters/HereNowdoc.xml new file mode 100644 index 0000000..18b3e47 --- /dev/null +++ b/src/config/filters/HereNowdoc.xml @@ -0,0 +1,20 @@ + + + + + + + + + + 1 + + + + + + diff --git a/src/lib/PreCommit/Filter/HereNowdoc.php b/src/lib/PreCommit/Filter/HereNowdoc.php new file mode 100644 index 0000000..a3e2cf5 --- /dev/null +++ b/src/lib/PreCommit/Filter/HereNowdoc.php @@ -0,0 +1,66 @@ +findTextBlockTags($content) as $tag) { + $content = $this->cut($tag, $content); + } + + return $content; + } + + /** + * Cut text code block + * + * @param string $tag + * @param string $content + * @return string + */ + protected function cut($tag, $content) + { + return preg_replace( + '/(=[ ]*)<<<[\'"]?'.$tag.'[\'"]?\r?\n(\r|\n|.)*?'.$tag.';/', + '$1\'\'; //replaced code because skipped validation', + $content + ); + } + + /** + * Find text block tags + * + * @param string $content + * @return array + */ + protected function findTextBlockTags($content) + { + //find blocks + preg_match_all('/=[ ]*<<<[\'"]?([A-z0-9]+)[\'"]?\r?\n/', $content, $matches); + + return empty($matches[1]) ? [] : array_unique($matches[1]); + } +}