Skip to content

Commit

Permalink
Implemented #128: Disable validation for heredoc and nowdoc in PHP code.
Browse files Browse the repository at this point in the history
 - Added cutting heredoc and nowdoc in PHP files.
  • Loading branch information
andkirby committed Nov 2, 2016
1 parent e572536 commit 65baba0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/config/filters/HereNowdoc.xml
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>
66 changes: 66 additions & 0 deletions src/lib/PreCommit/Filter/HereNowdoc.php
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]);
}
}

0 comments on commit 65baba0

Please sign in to comment.