Skip to content

Commit

Permalink
Filter out heredocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Hofmann committed Sep 17, 2015
1 parent 004fd55 commit 79b3f01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/rg/tools/phpnsc/ClassScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private function cleanContent($fileContent) {
$fileContent = $cleanWithWhitespaces("/(\'.*\')/sU", $fileContent);
$fileContent = $cleanWithWhitespaces("/(\".*\")/sU", $fileContent);
$fileContent = $cleanWithWhitespaces("/(\/\/.*)/", $fileContent);
$fileContent = $cleanWithWhitespaces("/(<<<(?P<tag>[A-Za-z_]+).*\g<tag>;)/sU", $fileContent);

if (false) {
$fileContent = preg_replace("/(\/\*.*\*\/)/sU", '', $fileContent);
Expand Down
63 changes: 36 additions & 27 deletions test/rg/tools/phpnsc/ClassScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ class ClassScannerTest extends PHPUnit_Framework_TestCase
{
/**
*
* @var ClassScannerFilesystemMock
* @var ClassScannerFilesystemMock
*/
private $filesystem;
/**
*
* @var rg\tools\phpnsc\ClassScanner
* @var rg\tools\phpnsc\ClassScanner
*/
private $classScanner;

protected function setUp() {
parent::setUp();
$output = new Symfony\Component\Console\Output\NullOutput();
$outputClass = new rg\tools\phpnsc\ConsoleOutput($output);
$this->filesystem = new ClassScannerFilesystemMock('/root/folder');
$this->classScanner = new rg\tools\phpnsc\ClassScanner($this->filesystem, '/root/folder',
$this->classScanner = new rg\tools\phpnsc\ClassScanner($this->filesystem, '/root/folder',
'vendor', $outputClass);
}

public function testParseDefinedEntities() {
$this->filesystem->filesystem = array(
'/root/folder/namespace/ClassOne.php' => '
Expand All @@ -48,16 +48,16 @@ class %%^daga
abstract class ClassTwo
interface InterfaceTwo
',
',
'/root/folder/namespace/InterfaceOne.php' => '
<?php
interface InterfaceOne
',
',
);
$files = array_keys($this->filesystem->filesystem);

$this->classScanner->parseFilesForClassesAndInterfaces($files);

$expectedEntities = array(
'ClassOne' => array(
'namespaces' => array('vendor\namespace'),
Expand All @@ -75,10 +75,10 @@ interface InterfaceOne
'namespaces' => array('vendor\namespace'),
),
);

$this->assertEquals($expectedEntities, $this->classScanner->getDefinedEntities());
}

public function testParseDefinedEntitiesWithDoubleEntityRaisesException() {
$this->filesystem->filesystem = array(
'/root/folder/namespace/ClassOne.php' => '
Expand All @@ -95,12 +95,12 @@ class %%^daga
abstract class ClassOne
interface InterfaceTwo
',
',
);
$files = array_keys($this->filesystem->filesystem);

$this->classScanner->parseFilesForClassesAndInterfaces($files);

$expectedEntities = array(
'ClassOne' => array(
'namespaces' => array('vendor\namespace', 'vendor\namespaceTwo'),
Expand All @@ -109,10 +109,10 @@ interface InterfaceTwo
'namespaces' => array('vendor\namespaceTwo'),
),
);

$this->assertEquals($expectedEntities, $this->classScanner->getDefinedEntities());
}

public function testParseUsedEntities() {
$this->filesystem->filesystem = array(
'/root/folder/namespace/ClassOne.php' => '
Expand All @@ -121,30 +121,39 @@ class ClassOne implements Bar extends Foo
',
'/root/folder/namespace/ClassTwo.php' => '
<?php
abstract class ClassTwo implements Bar
abstract class ClassTwo implements Bar
$bar = "new Foo"; $bar = \'new Foo\';
$bar = <<<_TEXT
new Foo();
_TEXT;
$foo = new ClassOne(ClassThree::CONSTANT, TypeHintClass $variable);
$bar = new ClassTwo;
$foo = new __CLASS__;
$xyz = new $variable(NotClassCONSTANT, parent::FOO, self::FOO, static::FOO, array $bar);
$b = new ClassTwo(ClassTwo::CONSTANT, OutOfNamespace $foo, OtherNamespace $bar, OutOfNamespace::FOO);
interface InterfaceTwo
',
',
);
$files = array_keys($this->filesystem->filesystem);

$this->classScanner->parseFilesForClassesAndInterfaces($files);

$expectedEntities = array(
'/root/folder/namespace/ClassOne.php' => array(
'Foo' => array(3), 'Bar' => array(3),
'Foo' => array(3),
'Bar' => array(3),
),
'/root/folder/namespace/ClassTwo.php' => array(
'ClassOne' => array(5), 'ClassTwo' => array(6,9), 'ClassThree' => array(5), 'OutOfNamespace' => array(9), 'TypeHintClass' => array(5),
'OtherNamespace' => array(9), 'Bar' => array(3),
'ClassOne' => array(8),
'ClassTwo' => array(9,12),
'ClassThree' => array(8),
'OutOfNamespace' => array(12),
'TypeHintClass' => array(8),
'OtherNamespace' => array(12),
'Bar' => array(3),
),
);

foreach ($files as $file) {
$this->assertEquals($expectedEntities[$file], $this->classScanner->getUsedEntities($file));
}
Expand All @@ -154,8 +163,8 @@ interface InterfaceTwo
class ClassScannerFilesystemMock extends \rg\tools\phpnsc\FilesystemAccess
{
public $filesystem = array();

public function getFile($filename) {
return $this->filesystem[$filename];
}
}
}

0 comments on commit 79b3f01

Please sign in to comment.