-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented #128: Disable validation for heredoc and nowdoc in PHP code.
- Added cutting heredoc and nowdoc in PHP files.
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* @license https://raw.githubusercontent.com/andkirby/commithook/master/LICENSE.md | ||
*/ | ||
--> | ||
<config> | ||
<!--Hooks process setup--> | ||
<hooks> | ||
<pre-commit> | ||
<filetype> | ||
<php> | ||
<filters> | ||
<HereNowdoc>1</HereNowdoc> | ||
</filters> | ||
</php> | ||
</filetype> | ||
</pre-commit> | ||
</hooks> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* @license https://raw.githubusercontent.com/andkirby/commithook/master/LICENSE.md | ||
*/ | ||
/** | ||
* Created by PhpStorm. | ||
* User: a.roslik | ||
* Date: 11/2/16 002 | ||
* Time: 6:06 PM | ||
*/ | ||
|
||
namespace PreCommit\Filter; | ||
|
||
/** | ||
* Class HereNowdoc filter | ||
* | ||
* @package AndKirby\PreCommit\Filter | ||
*/ | ||
class HereNowdoc | ||
{ | ||
/** | ||
* Filter skipped code blocks | ||
* | ||
* @param string $content | ||
* @param string|null $file | ||
* @return bool | ||
*/ | ||
public function filter($content, $file = null) | ||
{ | ||
foreach ($this->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]); | ||
} | ||
} |