From a0d1f0b095b4a3558afc5ed3682f25cac524a0ce Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 07:02:44 +0700 Subject: [PATCH 01/51] add DirectorySourceLocator and it's documentation and test case --- docs/usage.md | 5 ++ .../Type/DirectorySourceLocator.php | 82 +++++++++++++++++++ .../DirectoryScannerAssets/Bar/FooBar.php | 15 ++++ .../Assets/DirectoryScannerAssets/Foo.php | 15 ++++ .../Type/DirectorySourceLocatorTest.php | 27 ++++++ 5 files changed, 144 insertions(+) create mode 100644 src/SourceLocator/Type/DirectorySourceLocator.php create mode 100644 test/unit/Assets/DirectoryScannerAssets/Bar/FooBar.php create mode 100644 test/unit/Assets/DirectoryScannerAssets/Foo.php create mode 100644 test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php diff --git a/docs/usage.md b/docs/usage.md index 3e0f36f47..2516c988b 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -93,6 +93,11 @@ within the `Reflector`s. The library comes bundled with the following * `AggregateSourceLocator` - a combination of multiple `SourceLocator`s which are hunted through in the given order to locate the source. + + * `DirectorySourceLocator` - scan directories and create multiple `SingleFileSourceLocator` + for all found php files and wrapped into `AggregateSourceLocator` + + A `SourceLocator` is a callable, which when invoked must be given an `Identifier` (which describes a class/function/etc.). The `SourceLocator` diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php new file mode 100644 index 000000000..885d400d3 --- /dev/null +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -0,0 +1,82 @@ +scan($dir)); + } + + $this->aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); + + } + + /** + * scan target directory and resulted as SourceLocator[] + * @param $dir string directory path + * @return SourceLocator[] + */ + private function scan($dir){ + $sourceLocators = []; + $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); + foreach ( new \RecursiveIteratorIterator($rdi) as $item) { + if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { + $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); + } + } + return $sourceLocators; + } + + /** + * {@inheritDoc} + */ + public function locateIdentifier(Reflector $reflector, Identifier $identifier) + { + return $this->aggregatedSourceLocator->locateIdentifier($reflector, $identifier); + } + + /** + * {@inheritDoc} + */ + public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType) + { + return $this->aggregatedSourceLocator->locateIdentifiersByType($reflector, $identifierType); + } + + +} \ No newline at end of file diff --git a/test/unit/Assets/DirectoryScannerAssets/Bar/FooBar.php b/test/unit/Assets/DirectoryScannerAssets/Bar/FooBar.php new file mode 100644 index 000000000..61c4c39fa --- /dev/null +++ b/test/unit/Assets/DirectoryScannerAssets/Bar/FooBar.php @@ -0,0 +1,15 @@ +assertEquals(2, count($reflector->getAllClasses())); + } + + +} \ No newline at end of file From a7cd18c8a9c2a231610935d77d4936f0a5638227 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 08:41:22 +0700 Subject: [PATCH 02/51] fix code convention, code improvement and add more test case --- .../Exception/InvalidDirectory.php | 7 +++ .../Type/DirectorySourceLocator.php | 38 ++++++---------- .../DirectoryScannerAssets/Bar/Empty.php | 4 ++ .../DirectoryScannerAssets/Bar/FooBar.php | 8 ---- .../DirectoryScannerAssets/Bar/FooBar.tmp | 1 + .../Assets/DirectoryScannerAssets/Foo.php | 8 ---- .../Assets/DirectoryScannerAssets/Foo.tmp | 1 + .../Type/DirectorySourceLocatorTest.php | 43 +++++++++++++------ 8 files changed, 56 insertions(+), 54 deletions(-) create mode 100644 src/SourceLocator/Exception/InvalidDirectory.php create mode 100644 test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php create mode 100644 test/unit/Assets/DirectoryScannerAssets/Bar/FooBar.tmp create mode 100644 test/unit/Assets/DirectoryScannerAssets/Foo.tmp diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php new file mode 100644 index 000000000..ec39fbee0 --- /dev/null +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -0,0 +1,7 @@ +scan($dir)); } - $this->aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); - } /** @@ -51,7 +39,7 @@ public function __construct($directory = null) * @param $dir string directory path * @return SourceLocator[] */ - private function scan($dir){ + private function scan($dir) { $sourceLocators = []; $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); foreach ( new \RecursiveIteratorIterator($rdi) as $item) { @@ -77,6 +65,4 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id { return $this->aggregatedSourceLocator->locateIdentifiersByType($reflector, $identifierType); } - - } \ No newline at end of file diff --git a/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php b/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php new file mode 100644 index 000000000..e5d2a955c --- /dev/null +++ b/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php @@ -0,0 +1,4 @@ +assertEquals(2, count($reflector->getAllClasses())); + /** + * @var DirectorySourceLocator + */ + private $sourceLocator; + + public function setUp() + { + $this->sourceLocator = new DirectorySourceLocator([$this->directoryToScan]); } + public function testScanDirectoryClasses() + { + $reflector = new ClassReflector($this->sourceLocator); + $this->assertCount(2, $reflector->getAllClasses()); + } + public function testScanDirecotryFiels(){ + $class = new \ReflectionClass(get_class($this->sourceLocator)); + $method = $class->getMethod('scan'); + $method->setAccessible(true); + $result = $method->invokeArgs($this->sourceLocator, [$this->directoryToScan]); + $this->assertCount(3, $result); + } + + /** + * @expectedException \BetterReflection\SourceLocator\Exception\InvalidDirectory + */ + public function testInvalidDirectory(){ + new DirectorySourceLocator([substr($this->directoryToScan,0, strlen($this->directoryToScan)-1)]); + } } \ No newline at end of file From cfa5debc46f8c392f0398115147e659705813fe0 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 08:44:53 +0700 Subject: [PATCH 03/51] fix test method name --- test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index 0904ba745..1dfd5e5ac 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -29,7 +29,7 @@ public function testScanDirectoryClasses() $this->assertCount(2, $reflector->getAllClasses()); } - public function testScanDirecotryFiels(){ + public function testScanDirecotryFiles(){ $class = new \ReflectionClass(get_class($this->sourceLocator)); $method = $class->getMethod('scan'); $method->setAccessible(true); From 234ebbad005d590578ff1f86d3c46daafbc48532 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 08:56:00 +0700 Subject: [PATCH 04/51] fix code convention --- test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index 1dfd5e5ac..f25408d15 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -41,6 +41,6 @@ public function testScanDirecotryFiles(){ * @expectedException \BetterReflection\SourceLocator\Exception\InvalidDirectory */ public function testInvalidDirectory(){ - new DirectorySourceLocator([substr($this->directoryToScan,0, strlen($this->directoryToScan)-1)]); + new DirectorySourceLocator([substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]); } } \ No newline at end of file From ea32c358bdbf845a30bb55d188eb3393a5c297fd Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 08:57:12 +0700 Subject: [PATCH 05/51] fix code convention --- src/SourceLocator/Type/DirectorySourceLocator.php | 1 - test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php index 04d72c589..fc3602f9c 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -12,7 +12,6 @@ */ class DirectorySourceLocator implements SourceLocator { - /** * @var AggregateSourceLocator */ diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index f25408d15..e3b98bd79 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -10,7 +10,9 @@ */ class DirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { - + /** + * @var string + */ private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; /** From af74bdbed5392228912a6a926e66af67d5f219bb Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 09:57:46 +0700 Subject: [PATCH 06/51] fix code and code convention --- .../Exception/InvalidDirectory.php | 39 +++++++++- .../Type/DirectorySourceLocator.php | 14 ++-- .../Type/DirectorySourceLocatorTest.php | 72 +++++++++++++++++-- 3 files changed, 112 insertions(+), 13 deletions(-) diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index ec39fbee0..9efecb417 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -2,6 +2,43 @@ namespace BetterReflection\SourceLocator\Exception; -class InvalidDirectory extends \RuntimeException +class InvalidDirectory extends \RuntimeException { + /** + * @param $nonDirectory + * @return InvalidDirectory + */ + public static function fromNonDirectory($nonDirectory) + { + return new InvalidDirectory(sprintf('%s is not a directory', $nonDirectory)); + } + + /** + * @param $nonStringValue + * @return InvalidDirectory + */ + public static function fromNonStringValue($nonStringValue) + { + $foundType = null; + switch (true) { + case is_object($nonStringValue) : + $foundType = sprintf('class %s', get_class($nonStringValue)); + break; + case is_bool($nonStringValue) : + $foundType = 'boolean'; + break; + case is_null($nonStringValue) : + $foundType = 'null'; + break; + case is_int($nonStringValue) : + $foundType = 'integer'; + break; + case is_double($nonStringValue) : + $foundType = 'double'; + break; + default : + $foundType = 'unknown type'; + } + return new InvalidDirectory(sprintf('Expected string type of directory, %s given', $foundType)); + } } \ No newline at end of file diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php index fc3602f9c..12a5eb9ed 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -8,7 +8,7 @@ use BetterReflection\SourceLocator\Exception\InvalidDirectory; /** - * This source locator loads all php files in an entire directories. + * This source locator loads all php files in an entire directory or multiple directories. */ class DirectorySourceLocator implements SourceLocator { @@ -18,15 +18,16 @@ class DirectorySourceLocator implements SourceLocator private $aggregatedSourceLocator; /** - * @param $directories array directories to scan + * @param $directories string[] directories to scan */ public function __construct(array $directories) { $sourceLocators = []; foreach ($directories as $dir) { - $dir = (string) $dir; - if (!is_dir($dir)) { - throw new InvalidDirectory(sprintf('Is not a directory: %s', $dir)); + if (!is_string($dir)) { + throw InvalidDirectory::fromNonStringValue($dir); + } elseif (!is_dir($dir)) { + throw InvalidDirectory::fromNonDirectory($dir); } $sourceLocators = array_merge($sourceLocators, $this->scan($dir)); } @@ -38,7 +39,8 @@ public function __construct(array $directories) * @param $dir string directory path * @return SourceLocator[] */ - private function scan($dir) { + private function scan($dir) + { $sourceLocators = []; $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); foreach ( new \RecursiveIteratorIterator($rdi) as $item) { diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index e3b98bd79..dd54ce8e4 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -3,10 +3,12 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; +use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Type\DirectorySourceLocator; /** * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator + * @covers \BetterReflection\SourceLocator\Exception\InvalidDirectory */ class DirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { @@ -28,10 +30,19 @@ public function setUp() public function testScanDirectoryClasses() { $reflector = new ClassReflector($this->sourceLocator); - $this->assertCount(2, $reflector->getAllClasses()); + $classes = $reflector->getAllClasses(); + $this->assertCount(2, $classes); + $classNames = []; + foreach ($classes as $clazz) { + $classNames[] = $clazz->getName(); + } + sort($classNames); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); } - public function testScanDirecotryFiles(){ + public function testScanDirectoryFiles() + { $class = new \ReflectionClass(get_class($this->sourceLocator)); $method = $class->getMethod('scan'); $method->setAccessible(true); @@ -39,10 +50,59 @@ public function testScanDirecotryFiles(){ $this->assertCount(3, $result); } - /** - * @expectedException \BetterReflection\SourceLocator\Exception\InvalidDirectory - */ - public function testInvalidDirectory(){ + public function testInvalidDirectory() + { + $this->expectException(InvalidDirectory::class); new DirectorySourceLocator([substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]); } + + public function testInvalidIntegerDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, 1]); + } + + public function testInvalidBoleanDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, true]); + } + + public function testInvalidObjectDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, new \stdClass()]); + } + + public function testInvalidNullDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, null]); + } + + public function testExceptionMessage() + { + $e = InvalidDirectory::fromNonDirectory('testDir'); + $this->assertEquals(sprintf('%s is not a directory', 'testDir'), $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(new \stdClass()); + $expected = 'Expected string type of directory, class stdClass given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(true); + $expected = 'Expected string type of directory, boolean given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(null); + $expected = 'Expected string type of directory, null given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100); + $expected = 'Expected string type of directory, integer given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100.35); + $expected = 'Expected string type of directory, double given'; + $this->assertEquals($expected, $e->getMessage()); + } } \ No newline at end of file From 828663c1d7d0006a289dae95b6b682b26b38030b Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 10:10:33 +0700 Subject: [PATCH 07/51] add test case --- .../Type/DirectorySourceLocatorTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index dd54ce8e4..cb9b3027c 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -2,9 +2,11 @@ namespace BetterReflectionTest\SourceLocator\Type; +use BetterReflection\Reflection\Adapter\ReflectionClass; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Type\DirectorySourceLocator; +use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; /** * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator @@ -48,6 +50,33 @@ public function testScanDirectoryFiles() $method->setAccessible(true); $result = $method->invokeArgs($this->sourceLocator, [$this->directoryToScan]); $this->assertCount(3, $result); + + // test file path + $files = []; + foreach ($result as $file) { + $clazz = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); + $property = $clazz->getProperty('filename'); + $property->setAccessible(true); + $files[] = realpath($property->getValue($file)); + } + sort($files); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); + + // test class names + $classNames = []; + foreach ($result as $file) { + /* @var $file SingleFileSourceLocator */ + $reflector = new ClassReflector($file); + foreach ($reflector->getAllClasses() as $clazz) { + $classNames[] = $clazz->getName(); + } + } + sort($classNames); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + $this->assertCount(2, $classNames); } public function testInvalidDirectory() From 50beb3d7388ca1419a4047654ba307f8f38ff0ac Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 10:11:53 +0700 Subject: [PATCH 08/51] remove unused class --- test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index cb9b3027c..0d5bd54da 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -2,7 +2,6 @@ namespace BetterReflectionTest\SourceLocator\Type; -use BetterReflection\Reflection\Adapter\ReflectionClass; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Type\DirectorySourceLocator; From 27022f0e9d84ce5186af6fd5a796b59bbfa0ea5d Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 23:06:27 +0700 Subject: [PATCH 09/51] - add FileSystemIteratorSourceLocator and it's test case - refactor DirectorySourceLocator to use FileSystemIteratorSourceLocator - change test case for DirectorySourceLocator to scan multiple directories - add documentation for FileSystemIteratorSourceLocator --- docs/usage.md | 8 +- .../Exception/InvalidDirectory.php | 3 + .../Type/DirectorySourceLocator.php | 19 +---- .../Type/FileSystemIteratorSourceLocator.php | 69 ++++++++++++++++ .../DirectoryScannerAssetsFoo/Bar/Empty.php | 4 + .../DirectoryScannerAssetsFoo/Bar/FooBar.php | 8 ++ .../DirectoryScannerAssetsFoo/Bar/FooBar.tmp | 1 + .../Assets/DirectoryScannerAssetsFoo/Foo.php | 8 ++ .../Assets/DirectoryScannerAssetsFoo/Foo.tmp | 1 + .../Type/DirectorySourceLocatorTest.php | 57 ++++--------- .../FileSystemIteratorSourceLocatorTest.php | 80 +++++++++++++++++++ 11 files changed, 195 insertions(+), 63 deletions(-) create mode 100644 src/SourceLocator/Type/FileSystemIteratorSourceLocator.php create mode 100644 test/unit/Assets/DirectoryScannerAssetsFoo/Bar/Empty.php create mode 100644 test/unit/Assets/DirectoryScannerAssetsFoo/Bar/FooBar.php create mode 100644 test/unit/Assets/DirectoryScannerAssetsFoo/Bar/FooBar.tmp create mode 100644 test/unit/Assets/DirectoryScannerAssetsFoo/Foo.php create mode 100644 test/unit/Assets/DirectoryScannerAssetsFoo/Foo.tmp create mode 100644 test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php diff --git a/docs/usage.md b/docs/usage.md index 2516c988b..3a85a8f50 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -94,8 +94,12 @@ within the `Reflector`s. The library comes bundled with the following * `AggregateSourceLocator` - a combination of multiple `SourceLocator`s which are hunted through in the given order to locate the source. - * `DirectorySourceLocator` - scan directories and create multiple `SingleFileSourceLocator` - for all found php files and wrapped into `AggregateSourceLocator` + * `FileSystemIteratorSourceLocator` - uses `SPL FileSystemIterator` to iterate over files + and create multiple `SingleFileSourceLocator` then wrapped into `AggregateSourceLocator` + + * `DirectorySourceLocator` - scan directories using `SPL RecursiveDirectoryIterator` + and create multiple `FileSystemIteratorSourceLocator` + then wrapped into `AggregateSourceLocator` diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 9efecb417..8ddf99631 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -36,6 +36,9 @@ public static function fromNonStringValue($nonStringValue) case is_double($nonStringValue) : $foundType = 'double'; break; + case is_array($nonStringValue) : + $foundType = 'array'; + break; default : $foundType = 'unknown type'; } diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php index 12a5eb9ed..7ec8ddb13 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -29,28 +29,11 @@ public function __construct(array $directories) } elseif (!is_dir($dir)) { throw InvalidDirectory::fromNonDirectory($dir); } - $sourceLocators = array_merge($sourceLocators, $this->scan($dir)); + $sourceLocators[] = new FileSystemIteratorSourceLocator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS)); } $this->aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); } - /** - * scan target directory and resulted as SourceLocator[] - * @param $dir string directory path - * @return SourceLocator[] - */ - private function scan($dir) - { - $sourceLocators = []; - $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); - foreach ( new \RecursiveIteratorIterator($rdi) as $item) { - if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { - $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); - } - } - return $sourceLocators; - } - /** * {@inheritDoc} */ diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php new file mode 100644 index 000000000..abb9a8a10 --- /dev/null +++ b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php @@ -0,0 +1,69 @@ +fileSystemIterator = $filesystemIterator; + } + + private function getAggregatedSourceLocator() + { + if (null==$this->_aggregatedSourceLocator) { + $sourceLocators = $this->scan(); + $this->_aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); + } + return $this->_aggregatedSourceLocator; + } + + /** + * scan target directory and resulted as SourceLocator[] + * @return SourceLocator[] + */ + private function scan() + { + $sourceLocators = []; + foreach ( new \RecursiveIteratorIterator($this->fileSystemIterator) as $item ) { + if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { + $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); + } + } + return $sourceLocators; + } + + /** + * {@inheritDoc} + */ + public function locateIdentifier(Reflector $reflector, Identifier $identifier) + { + return $this->getAggregatedSourceLocator()->locateIdentifier($reflector, $identifier); + } + + /** + * {@inheritDoc} + */ + public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType) + { + return $this->getAggregatedSourceLocator()->locateIdentifiersByType($reflector, $identifierType); + } + +} \ No newline at end of file diff --git a/test/unit/Assets/DirectoryScannerAssetsFoo/Bar/Empty.php b/test/unit/Assets/DirectoryScannerAssetsFoo/Bar/Empty.php new file mode 100644 index 000000000..e5d2a955c --- /dev/null +++ b/test/unit/Assets/DirectoryScannerAssetsFoo/Bar/Empty.php @@ -0,0 +1,4 @@ +sourceLocator = new DirectorySourceLocator([$this->directoryToScan]); + $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssets'; + $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssetsFoo'; + $this->sourceLocator = new DirectorySourceLocator($this->directoryToScan); } public function testScanDirectoryClasses() { $reflector = new ClassReflector($this->sourceLocator); $classes = $reflector->getAllClasses(); - $this->assertCount(2, $classes); + $this->assertCount(4, $classes); $classNames = []; foreach ($classes as $clazz) { $classNames[] = $clazz->getName(); } sort($classNames); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); - } - - public function testScanDirectoryFiles() - { - $class = new \ReflectionClass(get_class($this->sourceLocator)); - $method = $class->getMethod('scan'); - $method->setAccessible(true); - $result = $method->invokeArgs($this->sourceLocator, [$this->directoryToScan]); - $this->assertCount(3, $result); - - // test file path - $files = []; - foreach ($result as $file) { - $clazz = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); - $property = $clazz->getProperty('filename'); - $property->setAccessible(true); - $files[] = realpath($property->getValue($file)); - } - sort($files); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); - - // test class names - $classNames = []; - foreach ($result as $file) { - /* @var $file SingleFileSourceLocator */ - $reflector = new ClassReflector($file); - foreach ($reflector->getAllClasses() as $clazz) { - $classNames[] = $clazz->getName(); - } - } - sort($classNames); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); - $this->assertCount(2, $classNames); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssetsFoo\Bar\FooBar', $classNames[0]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssetsFoo\Foo', $classNames[1]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[2]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[3]); } public function testInvalidDirectory() @@ -132,5 +99,9 @@ public function testExceptionMessage() $e = InvalidDirectory::fromNonStringValue(100.35); $expected = 'Expected string type of directory, double given'; $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue([100, 200]); + $expected = 'Expected string type of directory, array given'; + $this->assertEquals($expected, $e->getMessage()); } } \ No newline at end of file diff --git a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php new file mode 100644 index 000000000..f8430f2a2 --- /dev/null +++ b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php @@ -0,0 +1,80 @@ +directoryToScan, \RecursiveDirectoryIterator::SKIP_DOTS); + $this->sourceLocator = new FileSystemIteratorSourceLocator($fileSystemIterator); + } + + public function testScanDirectoryClasses() + { + $reflector = new ClassReflector($this->sourceLocator); + $classes = $reflector->getAllClasses(); + $this->assertCount(2, $classes); + $classNames = []; + foreach ($classes as $clazz) { + $classNames[] = $clazz->getName(); + } + sort($classNames); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + } + + public function testScanDirectoryFiles() + { + $fileSystemIteratorSourceLocator = $this->sourceLocator; + $class = new \ReflectionClass(get_class($fileSystemIteratorSourceLocator)); + $method = $class->getMethod('scan'); + $method->setAccessible(true); + $result = $method->invoke($fileSystemIteratorSourceLocator); + $this->assertCount(3, $result); + + // test file path + $files = []; + foreach ($result as $file) { + $clazz = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); + $property = $clazz->getProperty('filename'); + $property->setAccessible(true); + $files[] = realpath($property->getValue($file)); + } + sort($files); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); + $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); + + // test class names + $classNames = []; + foreach ($result as $file) { + /* @var $file SingleFileSourceLocator */ + $reflector = new ClassReflector($file); + foreach ($reflector->getAllClasses() as $clazz) { + $classNames[] = $clazz->getName(); + } + } + sort($classNames); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + $this->assertCount(2, $classNames); + } +} \ No newline at end of file From 351d39e17117fdee9f22801e0f3760469122134f Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sat, 24 Sep 2016 23:08:53 +0700 Subject: [PATCH 10/51] code convention --- src/SourceLocator/Type/FileSystemIteratorSourceLocator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php index abb9a8a10..494eb5348 100644 --- a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php @@ -9,7 +9,7 @@ /** * This source locator loads all php files from \FileSystemIterator */ -class FileSystemIteratorSourceLocator implements SourceLocator +class FileSystemIteratorSourceLocator implements SourceLocator { /** * @var AggregateSourceLocator From 17dcca8de55c18f766933d796474e04440069158 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 03:25:09 +0700 Subject: [PATCH 11/51] - Refactor invalid directory test into InvalidDirectorySourceLocatorTest - Change FileSYstemIteratorSourceLocator constructor parameter from \FileSystemIterator to \Iterator --- .../Exception/InvalidDirectory.php | 36 +++------ .../Exception/InvalidFileInfo.php | 16 ++++ .../Type/DirectorySourceLocator.php | 3 +- .../Type/FileSystemIteratorSourceLocator.php | 27 ++++--- .../Type/DirectorySourceLocatorTest.php | 62 -------------- .../FileSystemIteratorSourceLocatorTest.php | 10 +-- .../InvalidDirectorySourceLocatorTest.php | 81 +++++++++++++++++++ 7 files changed, 130 insertions(+), 105 deletions(-) create mode 100644 src/SourceLocator/Exception/InvalidFileInfo.php create mode 100644 test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 8ddf99631..c5bfaa430 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -5,43 +5,25 @@ class InvalidDirectory extends \RuntimeException { /** - * @param $nonDirectory + * @param $nonDirectory string * @return InvalidDirectory */ public static function fromNonDirectory($nonDirectory) { - return new InvalidDirectory(sprintf('%s is not a directory', $nonDirectory)); + if (!file_exists($nonDirectory)) { + return new InvalidDirectory(sprintf('%s is not exists', $nonDirectory)); + } else { + return new InvalidDirectory(sprintf('%s is must to be a directory not a file', $nonDirectory)); + } } /** - * @param $nonStringValue + * @param $nonStringValue mixed * @return InvalidDirectory */ public static function fromNonStringValue($nonStringValue) { - $foundType = null; - switch (true) { - case is_object($nonStringValue) : - $foundType = sprintf('class %s', get_class($nonStringValue)); - break; - case is_bool($nonStringValue) : - $foundType = 'boolean'; - break; - case is_null($nonStringValue) : - $foundType = 'null'; - break; - case is_int($nonStringValue) : - $foundType = 'integer'; - break; - case is_double($nonStringValue) : - $foundType = 'double'; - break; - case is_array($nonStringValue) : - $foundType = 'array'; - break; - default : - $foundType = 'unknown type'; - } - return new InvalidDirectory(sprintf('Expected string type of directory, %s given', $foundType)); + $type = is_object($nonStringValue) ? get_class($nonStringValue) : gettype($nonStringValue) ; + return new InvalidDirectory(sprintf('Expected string type of directory, %s given', $type)); } } \ No newline at end of file diff --git a/src/SourceLocator/Exception/InvalidFileInfo.php b/src/SourceLocator/Exception/InvalidFileInfo.php new file mode 100644 index 000000000..ee9179c97 --- /dev/null +++ b/src/SourceLocator/Exception/InvalidFileInfo.php @@ -0,0 +1,16 @@ +aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); } diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php index 494eb5348..22a546125 100644 --- a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php @@ -5,6 +5,7 @@ use BetterReflection\Identifier\Identifier; use BetterReflection\Identifier\IdentifierType; use BetterReflection\Reflector\Reflector; +use BetterReflection\SourceLocator\Exception\InvalidFileInfo; /** * This source locator loads all php files from \FileSystemIterator @@ -17,22 +18,27 @@ class FileSystemIteratorSourceLocator implements SourceLocator private $_aggregatedSourceLocator; /** - * @var \FilesystemIterator + * @var \Iterator */ private $fileSystemIterator; - public function __construct(\FilesystemIterator $filesystemIterator) + public function __construct(\Iterator $fileInfoIterator) { - $this->fileSystemIterator = $filesystemIterator; + foreach ($fileInfoIterator as $fileInfo) { + if (!$fileInfo instanceof \SplFileInfo) { + throw InvalidFileInfo::fromNonSplFileInfo($fileInfo); + } + } + $this->fileSystemIterator = $fileInfoIterator; } + /** + * Get a AggregateSourceLocator, create it if null. + * @return AggregateSourceLocator + */ private function getAggregatedSourceLocator() { - if (null==$this->_aggregatedSourceLocator) { - $sourceLocators = $this->scan(); - $this->_aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); - } - return $this->_aggregatedSourceLocator; + return $this->_aggregatedSourceLocator ? $this->_aggregatedSourceLocator : new AggregateSourceLocator($this->scan()); } /** @@ -42,11 +48,12 @@ private function getAggregatedSourceLocator() private function scan() { $sourceLocators = []; - foreach ( new \RecursiveIteratorIterator($this->fileSystemIterator) as $item ) { + foreach ( $this->fileSystemIterator as $item ) { + /* @var $item \SplFileInfo */ if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); } - } + } return $sourceLocators; } diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index 056c2ad03..b547807ab 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -3,12 +3,10 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; -use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Type\DirectorySourceLocator; /** * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator - * @covers \BetterReflection\SourceLocator\Exception\InvalidDirectory */ class DirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { @@ -44,64 +42,4 @@ public function testScanDirectoryClasses() $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[2]); $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[3]); } - - public function testInvalidDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]); - } - - public function testInvalidIntegerDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, 1]); - } - - public function testInvalidBoleanDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, true]); - } - - public function testInvalidObjectDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, new \stdClass()]); - } - - public function testInvalidNullDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, null]); - } - - public function testExceptionMessage() - { - $e = InvalidDirectory::fromNonDirectory('testDir'); - $this->assertEquals(sprintf('%s is not a directory', 'testDir'), $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(new \stdClass()); - $expected = 'Expected string type of directory, class stdClass given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(true); - $expected = 'Expected string type of directory, boolean given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(null); - $expected = 'Expected string type of directory, null given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100); - $expected = 'Expected string type of directory, integer given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100.35); - $expected = 'Expected string type of directory, double given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue([100, 200]); - $expected = 'Expected string type of directory, array given'; - $this->assertEquals($expected, $e->getMessage()); - } } \ No newline at end of file diff --git a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php index f8430f2a2..b37509c49 100644 --- a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php @@ -24,7 +24,7 @@ class FileSystemIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase public function setUp() { $fileSystemIterator = new \RecursiveDirectoryIterator($this->directoryToScan, \RecursiveDirectoryIterator::SKIP_DOTS); - $this->sourceLocator = new FileSystemIteratorSourceLocator($fileSystemIterator); + $this->sourceLocator = new FileSystemIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); } public function testScanDirectoryClasses() @@ -53,8 +53,8 @@ public function testScanDirectoryFiles() // test file path $files = []; foreach ($result as $file) { - $clazz = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); - $property = $clazz->getProperty('filename'); + $class = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); + $property = $class->getProperty('filename'); $property->setAccessible(true); $files[] = realpath($property->getValue($file)); } @@ -68,8 +68,8 @@ public function testScanDirectoryFiles() foreach ($result as $file) { /* @var $file SingleFileSourceLocator */ $reflector = new ClassReflector($file); - foreach ($reflector->getAllClasses() as $clazz) { - $classNames[] = $clazz->getName(); + foreach ($reflector->getAllClasses() as $class) { + $classNames[] = $class->getName(); } } sort($classNames); diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php new file mode 100644 index 000000000..48b1c414f --- /dev/null +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -0,0 +1,81 @@ +expectException(InvalidDirectory::class); + new DirectorySourceLocator([substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]); + } + + public function testInvalidIntegerDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, 1]); + } + + public function testInvalidBoleanDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, true]); + } + + public function testInvalidObjectDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, new \stdClass()]); + } + + public function testInvalidNullDirectory() + { + $this->expectException(InvalidDirectory::class); + new DirectorySourceLocator([$this->directoryToScan, null]); + } + + public function testExceptionMessage() + { + $e = InvalidDirectory::fromNonDirectory('testDir'); + $this->assertEquals(sprintf('%s is not exists', 'testDir'), $e->getMessage()); + + $e = InvalidDirectory::fromNonDirectory(__FILE__); + $this->assertEquals(sprintf('%s is must to be a directory not a file', __FILE__), $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(new \stdClass()); + $expected = 'Expected string type of directory, stdClass given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(true); + $expected = 'Expected string type of directory, boolean given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(null); + $expected = 'Expected string type of directory, NULL given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100); + $expected = 'Expected string type of directory, integer given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100.35); + $expected = 'Expected string type of directory, double given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue([100, 200]); + $expected = 'Expected string type of directory, array given'; + $this->assertEquals($expected, $e->getMessage()); + } +} \ No newline at end of file From c1583123d9c4c8563be97edd7021b40df44a5392 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 04:33:36 +0700 Subject: [PATCH 12/51] PSR-2 --- src/SourceLocator/Exception/InvalidDirectory.php | 2 +- src/SourceLocator/Exception/InvalidFileInfo.php | 2 +- src/SourceLocator/Type/DirectorySourceLocator.php | 5 +++-- .../Type/FileSystemIteratorSourceLocator.php | 13 +++++++------ .../Type/DirectorySourceLocatorTest.php | 2 +- .../Type/FileSystemIteratorSourceLocatorTest.php | 7 +++++-- .../Type/InvalidDirectorySourceLocatorTest.php | 4 ++-- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index c5bfaa430..30b066d44 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -26,4 +26,4 @@ public static function fromNonStringValue($nonStringValue) $type = is_object($nonStringValue) ? get_class($nonStringValue) : gettype($nonStringValue) ; return new InvalidDirectory(sprintf('Expected string type of directory, %s given', $type)); } -} \ No newline at end of file +} diff --git a/src/SourceLocator/Exception/InvalidFileInfo.php b/src/SourceLocator/Exception/InvalidFileInfo.php index ee9179c97..7e8fbb94b 100644 --- a/src/SourceLocator/Exception/InvalidFileInfo.php +++ b/src/SourceLocator/Exception/InvalidFileInfo.php @@ -13,4 +13,4 @@ public static function fromNonSplFileInfo($nonSplFileInfo) $type = is_object($nonSplFileInfo) ? get_class($nonSplFileInfo) : gettype($nonSplFileInfo) ; return new InvalidFileInfo(sprintf('Expected \\SplFileInfo type of Iterator\'s items, %s given', $type)); } -} \ No newline at end of file +} diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php index 474d38394..32e7e28ab 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -26,7 +26,8 @@ public function __construct(array $directories) foreach ($directories as $dir) { if (!is_string($dir)) { throw InvalidDirectory::fromNonStringValue($dir); - } elseif (!is_dir($dir)) { + } + if (!is_dir($dir)) { throw InvalidDirectory::fromNonDirectory($dir); } $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); @@ -50,4 +51,4 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id { return $this->aggregatedSourceLocator->locateIdentifiersByType($reflector, $identifierType); } -} \ No newline at end of file +} diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php index 22a546125..6f4ff917c 100644 --- a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php @@ -13,9 +13,10 @@ class FileSystemIteratorSourceLocator implements SourceLocator { /** + * Don't touch this variable directly * @var AggregateSourceLocator */ - private $_aggregatedSourceLocator; + private $aggregatedSourceLocator; /** * @var \Iterator @@ -38,7 +39,8 @@ public function __construct(\Iterator $fileInfoIterator) */ private function getAggregatedSourceLocator() { - return $this->_aggregatedSourceLocator ? $this->_aggregatedSourceLocator : new AggregateSourceLocator($this->scan()); + return $this->aggregatedSourceLocator ? + $this->aggregatedSourceLocator : new AggregateSourceLocator($this->scan()); } /** @@ -48,12 +50,12 @@ private function getAggregatedSourceLocator() private function scan() { $sourceLocators = []; - foreach ( $this->fileSystemIterator as $item ) { + foreach ($this->fileSystemIterator as $item) { /* @var $item \SplFileInfo */ if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); } - } + } return $sourceLocators; } @@ -72,5 +74,4 @@ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $id { return $this->getAggregatedSourceLocator()->locateIdentifiersByType($reflector, $identifierType); } - -} \ No newline at end of file +} diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index b547807ab..f7d04aa33 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -42,4 +42,4 @@ public function testScanDirectoryClasses() $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[2]); $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[3]); } -} \ No newline at end of file +} diff --git a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php index b37509c49..f0f400c3b 100644 --- a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php @@ -23,7 +23,10 @@ class FileSystemIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase public function setUp() { - $fileSystemIterator = new \RecursiveDirectoryIterator($this->directoryToScan, \RecursiveDirectoryIterator::SKIP_DOTS); + $fileSystemIterator = new \RecursiveDirectoryIterator( + $this->directoryToScan, + \RecursiveDirectoryIterator::SKIP_DOTS + ); $this->sourceLocator = new FileSystemIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); } @@ -77,4 +80,4 @@ public function testScanDirectoryFiles() $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); $this->assertCount(2, $classNames); } -} \ No newline at end of file +} diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php index 48b1c414f..7f5018e71 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -28,7 +28,7 @@ public function testInvalidIntegerDirectory() new DirectorySourceLocator([$this->directoryToScan, 1]); } - public function testInvalidBoleanDirectory() + public function testInvalidBooleanDirectory() { $this->expectException(InvalidDirectory::class); new DirectorySourceLocator([$this->directoryToScan, true]); @@ -78,4 +78,4 @@ public function testExceptionMessage() $expected = 'Expected string type of directory, array given'; $this->assertEquals($expected, $e->getMessage()); } -} \ No newline at end of file +} From ba00a6e403e504de6d36e653e52530f15065e145 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 04:42:29 +0700 Subject: [PATCH 13/51] refactor test case to use phpunit dataProvider --- .../InvalidDirectorySourceLocatorTest.php | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php index 7f5018e71..ec868ec61 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -16,34 +16,25 @@ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase */ private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; - public function testInvalidDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]); - } - - public function testInvalidIntegerDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, 1]); - } - - public function testInvalidBooleanDirectory() - { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, true]); - } - - public function testInvalidObjectDirectory() + /** + * @dataProvider invalidDirectoriesProvider + * @param array $directories + */ + public function testInvalidDirectory(array $directories) { $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, new \stdClass()]); + new DirectorySourceLocator($directories); } - public function testInvalidNullDirectory() + public function invalidDirectoriesProvider() { - $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator([$this->directoryToScan, null]); + return [ + [[substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]], + [[$this->directoryToScan, 1]], + [[$this->directoryToScan, true]], + [[$this->directoryToScan, new \stdClass()]], + [[$this->directoryToScan, null]], + ]; } public function testExceptionMessage() From 8759ac577c879d6f01a04e5d879563a8fb1961f3 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 04:49:45 +0700 Subject: [PATCH 14/51] Improve InvalidDirectorySourceLocatorTest --- .../Type/InvalidDirectorySourceLocatorTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php index ec868ec61..753e64d97 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -16,6 +16,14 @@ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase */ private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; + /** + * Make sure that $directoryToScan is a valid directory + */ + public function testDirectoryToScan() + { + new DirectorySourceLocator([$this->directoryToScan, $this->directoryToScan]); + } + /** * @dataProvider invalidDirectoriesProvider * @param array $directories @@ -29,7 +37,7 @@ public function testInvalidDirectory(array $directories) public function invalidDirectoriesProvider() { return [ - [[substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]], + [[$this->directoryToScan, substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]], [[$this->directoryToScan, 1]], [[$this->directoryToScan, true]], [[$this->directoryToScan, new \stdClass()]], From 67e66ffb424175576951cbc0ed0ef981a8acef95 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 06:43:08 +0700 Subject: [PATCH 15/51] fix code convention, variable naming and separate test case of exception in their own test case --- .../Exception/InvalidDirectory.php | 11 +++-- .../Exception/InvalidFileInfo.php | 4 +- .../Type/DirectorySourceLocator.php | 23 +++++----- .../Type/FileSystemIteratorSourceLocator.php | 15 ++++--- .../DirectoryScannerAssets/Bar/Empty.php | 2 +- .../DirectoryScannerAssets/Bar/FooBar.php | 2 +- .../DirectoryScannerAssets/Bar/FooBar.tmp | 2 +- .../Assets/DirectoryScannerAssets/Foo.php | 2 +- .../Assets/DirectoryScannerAssets/Foo.tmp | 2 +- .../DirectoryScannerAssetsFoo/Bar/Empty.php | 2 +- .../DirectoryScannerAssetsFoo/Bar/FooBar.php | 2 +- .../DirectoryScannerAssetsFoo/Bar/FooBar.tmp | 2 +- .../Assets/DirectoryScannerAssetsFoo/Foo.php | 2 +- .../Assets/DirectoryScannerAssetsFoo/Foo.tmp | 2 +- .../Exception/InvalidDirectoryTest.php | 44 +++++++++++++++++++ .../Exception/InvalidFileInfoTest.php | 38 ++++++++++++++++ .../Type/DirectorySourceLocatorTest.php | 4 +- .../FileSystemIteratorSourceLocatorTest.php | 4 +- .../InvalidDirectorySourceLocatorTest.php | 34 -------------- 19 files changed, 124 insertions(+), 73 deletions(-) create mode 100644 test/unit/SourceLocator/Exception/InvalidDirectoryTest.php create mode 100644 test/unit/SourceLocator/Exception/InvalidFileInfoTest.php diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 30b066d44..52cab526f 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -5,25 +5,24 @@ class InvalidDirectory extends \RuntimeException { /** - * @param $nonDirectory string + * @param string $nonDirectory * @return InvalidDirectory */ public static function fromNonDirectory($nonDirectory) { if (!file_exists($nonDirectory)) { - return new InvalidDirectory(sprintf('%s is not exists', $nonDirectory)); - } else { - return new InvalidDirectory(sprintf('%s is must to be a directory not a file', $nonDirectory)); + return new self(sprintf('%s does not exists', $nonDirectory)); } + return new self(sprintf('%s is must to be a directory not a file', $nonDirectory)); } /** - * @param $nonStringValue mixed + * @param mixed $nonStringValue * @return InvalidDirectory */ public static function fromNonStringValue($nonStringValue) { $type = is_object($nonStringValue) ? get_class($nonStringValue) : gettype($nonStringValue) ; - return new InvalidDirectory(sprintf('Expected string type of directory, %s given', $type)); + return new self(sprintf('Expected string, %s given', $type)); } } diff --git a/src/SourceLocator/Exception/InvalidFileInfo.php b/src/SourceLocator/Exception/InvalidFileInfo.php index 7e8fbb94b..2f977e1d8 100644 --- a/src/SourceLocator/Exception/InvalidFileInfo.php +++ b/src/SourceLocator/Exception/InvalidFileInfo.php @@ -5,12 +5,12 @@ class InvalidFileInfo extends \RuntimeException { /** - * @param $nonSplFileInfo mixed + * @param mixed $nonSplFileInfo * @return InvalidFileInfo */ public static function fromNonSplFileInfo($nonSplFileInfo) { $type = is_object($nonSplFileInfo) ? get_class($nonSplFileInfo) : gettype($nonSplFileInfo) ; - return new InvalidFileInfo(sprintf('Expected \\SplFileInfo type of Iterator\'s items, %s given', $type)); + return new self(sprintf('Expected an iterator of SplFileInfo instances, %s given instead', $type)); } } diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/DirectorySourceLocator.php index 32e7e28ab..786a95c79 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/DirectorySourceLocator.php @@ -15,25 +15,26 @@ class DirectorySourceLocator implements SourceLocator /** * @var AggregateSourceLocator */ - private $aggregatedSourceLocator; + private $aggregateSourceLocator; /** - * @param $directories string[] directories to scan + * @param string[] $directories directories to scan + * @throws InvalidDirectory */ public function __construct(array $directories) { $sourceLocators = []; - foreach ($directories as $dir) { - if (!is_string($dir)) { - throw InvalidDirectory::fromNonStringValue($dir); + foreach ($directories as $directory) { + if (!is_string($directory)) { + throw InvalidDirectory::fromNonStringValue($directory); } - if (!is_dir($dir)) { - throw InvalidDirectory::fromNonDirectory($dir); + if (!is_dir($directory)) { + throw InvalidDirectory::fromNonDirectory($directory); } - $rdi = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS); + $rdi = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); $sourceLocators[] = new FileSystemIteratorSourceLocator(new \RecursiveIteratorIterator($rdi)); } - $this->aggregatedSourceLocator = new AggregateSourceLocator($sourceLocators); + $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); } /** @@ -41,7 +42,7 @@ public function __construct(array $directories) */ public function locateIdentifier(Reflector $reflector, Identifier $identifier) { - return $this->aggregatedSourceLocator->locateIdentifier($reflector, $identifier); + return $this->aggregateSourceLocator->locateIdentifier($reflector, $identifier); } /** @@ -49,6 +50,6 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier) */ public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType) { - return $this->aggregatedSourceLocator->locateIdentifiersByType($reflector, $identifierType); + return $this->aggregateSourceLocator->locateIdentifiersByType($reflector, $identifierType); } } diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php index 6f4ff917c..e6096be46 100644 --- a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php @@ -13,16 +13,19 @@ class FileSystemIteratorSourceLocator implements SourceLocator { /** - * Don't touch this variable directly - * @var AggregateSourceLocator + * @var AggregateSourceLocator|null */ - private $aggregatedSourceLocator; + private $aggregateSourceLocator; /** * @var \Iterator */ private $fileSystemIterator; + /** + * @param \Iterator $fileInfoIterator + * @throws InvalidFileInfo In case of iterator not contains only SplFileInfo + */ public function __construct(\Iterator $fileInfoIterator) { foreach ($fileInfoIterator as $fileInfo) { @@ -39,8 +42,8 @@ public function __construct(\Iterator $fileInfoIterator) */ private function getAggregatedSourceLocator() { - return $this->aggregatedSourceLocator ? - $this->aggregatedSourceLocator : new AggregateSourceLocator($this->scan()); + return $this->aggregateSourceLocator ? + $this->aggregateSourceLocator : new AggregateSourceLocator($this->scan()); } /** @@ -52,7 +55,7 @@ private function scan() $sourceLocators = []; foreach ($this->fileSystemIterator as $item) { /* @var $item \SplFileInfo */ - if ($item->isFile() && pathinfo($item->getRealPath(), PATHINFO_EXTENSION) == 'php') { + if ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php') { $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); } } diff --git a/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php b/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php index e5d2a955c..03622f520 100644 --- a/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php +++ b/test/unit/Assets/DirectoryScannerAssets/Bar/Empty.php @@ -1,4 +1,4 @@ assertEquals(sprintf('%s does not exists', 'testDir'), $e->getMessage()); + + $e = InvalidDirectory::fromNonDirectory(__FILE__); + $this->assertEquals(sprintf('%s is must to be a directory not a file', __FILE__), $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(new \stdClass()); + $expected = 'Expected string, stdClass given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(true); + $expected = 'Expected string, boolean given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(null); + $expected = 'Expected string, NULL given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100); + $expected = 'Expected string, integer given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue(100.35); + $expected = 'Expected string, double given'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidDirectory::fromNonStringValue([100, 200]); + $expected = 'Expected string, array given'; + $this->assertEquals($expected, $e->getMessage()); + } +} diff --git a/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php b/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php new file mode 100644 index 000000000..9477f933b --- /dev/null +++ b/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php @@ -0,0 +1,38 @@ +assertEquals($expected, $e->getMessage()); + + $e = InvalidFileInfo::fromNonSplFileInfo(true); + $expected = 'Expected an iterator of SplFileInfo instances, boolean given instead'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidFileInfo::fromNonSplFileInfo(null); + $expected = 'Expected an iterator of SplFileInfo instances, NULL given instead'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidFileInfo::fromNonSplFileInfo(100); + $expected = 'Expected an iterator of SplFileInfo instances, integer given instead'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidFileInfo::fromNonSplFileInfo(100.35); + $expected = 'Expected an iterator of SplFileInfo instances, double given instead'; + $this->assertEquals($expected, $e->getMessage()); + + $e = InvalidFileInfo::fromNonSplFileInfo([100, 200]); + $expected = 'Expected an iterator of SplFileInfo instances, array given instead'; + $this->assertEquals($expected, $e->getMessage()); + } +} diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php index f7d04aa33..d29685796 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php @@ -33,8 +33,8 @@ public function testScanDirectoryClasses() $classes = $reflector->getAllClasses(); $this->assertCount(4, $classes); $classNames = []; - foreach ($classes as $clazz) { - $classNames[] = $clazz->getName(); + foreach ($classes as $class) { + $classNames[] = $class->getName(); } sort($classNames); $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssetsFoo\Bar\FooBar', $classNames[0]); diff --git a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php index f0f400c3b..7bdbb8dd6 100644 --- a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php @@ -36,8 +36,8 @@ public function testScanDirectoryClasses() $classes = $reflector->getAllClasses(); $this->assertCount(2, $classes); $classNames = []; - foreach ($classes as $clazz) { - $classNames[] = $clazz->getName(); + foreach ($classes as $class) { + $classNames[] = $class->getName(); } sort($classNames); $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php index 753e64d97..ef1c89a4b 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -7,7 +7,6 @@ /** * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator - * @covers \BetterReflection\SourceLocator\Exception\InvalidDirectory */ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { @@ -44,37 +43,4 @@ public function invalidDirectoriesProvider() [[$this->directoryToScan, null]], ]; } - - public function testExceptionMessage() - { - $e = InvalidDirectory::fromNonDirectory('testDir'); - $this->assertEquals(sprintf('%s is not exists', 'testDir'), $e->getMessage()); - - $e = InvalidDirectory::fromNonDirectory(__FILE__); - $this->assertEquals(sprintf('%s is must to be a directory not a file', __FILE__), $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(new \stdClass()); - $expected = 'Expected string type of directory, stdClass given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(true); - $expected = 'Expected string type of directory, boolean given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(null); - $expected = 'Expected string type of directory, NULL given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100); - $expected = 'Expected string type of directory, integer given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100.35); - $expected = 'Expected string type of directory, double given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue([100, 200]); - $expected = 'Expected string type of directory, array given'; - $this->assertEquals($expected, $e->getMessage()); - } } From 176c4565950bb140e8e2d3ec88b3e924d0d35f94 Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 07:17:24 +0700 Subject: [PATCH 16/51] refactor class naming including their documentation, also add sample codes for new classes --- docs/usage.md | 44 ++++++++++++++++--- ...ipleDirectoriesAggregateSourceLocator.php} | 4 +- ...r.php => SingleDirectorySourceLocator.php} | 2 +- .../InvalidDirectorySourceLocatorTest.php | 8 ++-- ...DirectoriesAggregateSourceLocatorTest.php} | 10 ++--- ...p => SingleDirectorySourceLocatorTest.php} | 10 ++--- 6 files changed, 54 insertions(+), 24 deletions(-) rename src/SourceLocator/Type/{DirectorySourceLocator.php => MultipleDirectoriesAggregateSourceLocator.php} (90%) rename src/SourceLocator/Type/{FileSystemIteratorSourceLocator.php => SingleDirectorySourceLocator.php} (97%) rename test/unit/SourceLocator/Type/{DirectorySourceLocatorTest.php => MultipleDirectoriesAggregateSourceLocatorTest.php} (74%) rename test/unit/SourceLocator/Type/{FileSystemIteratorSourceLocatorTest.php => SingleDirectorySourceLocatorTest.php} (87%) diff --git a/docs/usage.md b/docs/usage.md index 3a85a8f50..198738fa8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -94,15 +94,12 @@ within the `Reflector`s. The library comes bundled with the following * `AggregateSourceLocator` - a combination of multiple `SourceLocator`s which are hunted through in the given order to locate the source. - * `FileSystemIteratorSourceLocator` - uses `SPL FileSystemIterator` to iterate over files - and create multiple `SingleFileSourceLocator` then wrapped into `AggregateSourceLocator` + * `SingleDirectorySourceLocator` - iterates all files in a single directory + (and descends into child directories) - * `DirectorySourceLocator` - scan directories using `SPL RecursiveDirectoryIterator` - and create multiple `FileSystemIteratorSourceLocator` - then wrapped into `AggregateSourceLocator` + * `MultipleDirectoriesAggregateSourceLocator` - iterates a list of directories (and their children), + basically an aggregate version of `SingleDirectorySourceLocator` - - A `SourceLocator` is a callable, which when invoked must be given an `Identifier` (which describes a class/function/etc.). The `SourceLocator` should be written so that it returns a `Reflection` object directly. @@ -181,6 +178,39 @@ $reflector = new ClassReflector(new SingleFileSourceLocator('path/to/file.php')) $classes = $reflector->getAllClasses(); ``` +### Fetch reflections of all the classes in a directory + +```php +getAllClasses(); +``` + +### Fetch reflections of all the classes in a directory (including sub directories) + +```php +getAllClasses(); +``` + +### Fetch reflections of all the classes in multiple directories (including sub directories) + +```php +getAllClasses(); +``` + + ## Reflecting Functions The `FunctionReflector` is used to create Better Reflection `ReflectionFunction` diff --git a/src/SourceLocator/Type/DirectorySourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php similarity index 90% rename from src/SourceLocator/Type/DirectorySourceLocator.php rename to src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index 786a95c79..5900fbda8 100644 --- a/src/SourceLocator/Type/DirectorySourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -10,7 +10,7 @@ /** * This source locator loads all php files in an entire directory or multiple directories. */ -class DirectorySourceLocator implements SourceLocator +class MultipleDirectoriesAggregateSourceLocator implements SourceLocator { /** * @var AggregateSourceLocator @@ -32,7 +32,7 @@ public function __construct(array $directories) throw InvalidDirectory::fromNonDirectory($directory); } $rdi = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); - $sourceLocators[] = new FileSystemIteratorSourceLocator(new \RecursiveIteratorIterator($rdi)); + $sourceLocators[] = new SingleDirectorySourceLocator(new \RecursiveIteratorIterator($rdi)); } $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); } diff --git a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php b/src/SourceLocator/Type/SingleDirectorySourceLocator.php similarity index 97% rename from src/SourceLocator/Type/FileSystemIteratorSourceLocator.php rename to src/SourceLocator/Type/SingleDirectorySourceLocator.php index e6096be46..b1556e925 100644 --- a/src/SourceLocator/Type/FileSystemIteratorSourceLocator.php +++ b/src/SourceLocator/Type/SingleDirectorySourceLocator.php @@ -10,7 +10,7 @@ /** * This source locator loads all php files from \FileSystemIterator */ -class FileSystemIteratorSourceLocator implements SourceLocator +class SingleDirectorySourceLocator implements SourceLocator { /** * @var AggregateSourceLocator|null diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php index ef1c89a4b..0632ab664 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php @@ -3,10 +3,10 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\SourceLocator\Exception\InvalidDirectory; -use BetterReflection\SourceLocator\Type\DirectorySourceLocator; +use BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator + * @covers \BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator */ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { @@ -20,7 +20,7 @@ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase */ public function testDirectoryToScan() { - new DirectorySourceLocator([$this->directoryToScan, $this->directoryToScan]); + new MultipleDirectoriesAggregateSourceLocator([$this->directoryToScan, $this->directoryToScan]); } /** @@ -30,7 +30,7 @@ public function testDirectoryToScan() public function testInvalidDirectory(array $directories) { $this->expectException(InvalidDirectory::class); - new DirectorySourceLocator($directories); + new MultipleDirectoriesAggregateSourceLocator($directories); } public function invalidDirectoriesProvider() diff --git a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php similarity index 74% rename from test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php rename to test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php index d29685796..03859c332 100644 --- a/test/unit/SourceLocator/Type/DirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php @@ -3,12 +3,12 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; -use BetterReflection\SourceLocator\Type\DirectorySourceLocator; +use BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\DirectorySourceLocator + * @covers \BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator */ -class DirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase +class MultipleDirectoriesAggregateSourceLocatorTest extends \PHPUnit_Framework_TestCase { /** * @var string[] @@ -16,7 +16,7 @@ class DirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase private $directoryToScan = []; /** - * @var DirectorySourceLocator + * @var MultipleDirectoriesAggregateSourceLocator */ private $sourceLocator; @@ -24,7 +24,7 @@ public function setUp() { $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssets'; $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssetsFoo'; - $this->sourceLocator = new DirectorySourceLocator($this->directoryToScan); + $this->sourceLocator = new MultipleDirectoriesAggregateSourceLocator($this->directoryToScan); } public function testScanDirectoryClasses() diff --git a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php similarity index 87% rename from test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php rename to test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php index 7bdbb8dd6..b0e96a3ac 100644 --- a/test/unit/SourceLocator/Type/FileSystemIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php @@ -3,13 +3,13 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; -use BetterReflection\SourceLocator\Type\FileSystemIteratorSourceLocator; +use BetterReflection\SourceLocator\Type\SingleDirectorySourceLocator; use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\FileSystemIteratorSourceLocator + * @covers \BetterReflection\SourceLocator\Type\SingleDirectorySourceLocator */ -class FileSystemIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase +class SingleDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -17,7 +17,7 @@ class FileSystemIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; /** - * @var FileSystemIteratorSourceLocator + * @var SingleDirectorySourceLocator */ private $sourceLocator; @@ -27,7 +27,7 @@ public function setUp() $this->directoryToScan, \RecursiveDirectoryIterator::SKIP_DOTS ); - $this->sourceLocator = new FileSystemIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); + $this->sourceLocator = new SingleDirectorySourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); } public function testScanDirectoryClasses() From ac25eab519b47f814e99ec7fe234e074a25a2c7c Mon Sep 17 00:00:00 2001 From: Mapkuff Date: Sun, 25 Sep 2016 20:34:06 +0700 Subject: [PATCH 17/51] fix short variable naming. --- .../Type/MultipleDirectoriesAggregateSourceLocator.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index 5900fbda8..dee27e13d 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -31,8 +31,13 @@ public function __construct(array $directories) if (!is_dir($directory)) { throw InvalidDirectory::fromNonDirectory($directory); } - $rdi = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); - $sourceLocators[] = new SingleDirectorySourceLocator(new \RecursiveIteratorIterator($rdi)); + $recursiveDirectoryIterator = new \RecursiveDirectoryIterator( + $directory, + \RecursiveDirectoryIterator::SKIP_DOTS + ); + $sourceLocators[] = new SingleDirectorySourceLocator( + new \RecursiveIteratorIterator($recursiveDirectoryIterator) + ); } $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); } From 23b508e69f4328aae03e14a4bc559982f3627e18 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:17:20 +0200 Subject: [PATCH 18/51] #214 removed trailing whitespace --- docs/usage.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 198738fa8..9315993d8 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -93,13 +93,13 @@ within the `Reflector`s. The library comes bundled with the following * `AggregateSourceLocator` - a combination of multiple `SourceLocator`s which are hunted through in the given order to locate the source. - + * `SingleDirectorySourceLocator` - iterates all files in a single directory (and descends into child directories) - + * `MultipleDirectoriesAggregateSourceLocator` - iterates a list of directories (and their children), basically an aggregate version of `SingleDirectorySourceLocator` - + A `SourceLocator` is a callable, which when invoked must be given an `Identifier` (which describes a class/function/etc.). The `SourceLocator` should be written so that it returns a `Reflection` object directly. From ced49c260663275c06d039a899be841193345bbb Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:18:17 +0200 Subject: [PATCH 19/51] #214 CS (inlining expressions, docblock spacing, adding spacing) --- src/SourceLocator/Exception/InvalidDirectory.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 52cab526f..064add0f1 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -6,23 +6,28 @@ class InvalidDirectory extends \RuntimeException { /** * @param string $nonDirectory + * * @return InvalidDirectory */ public static function fromNonDirectory($nonDirectory) { - if (!file_exists($nonDirectory)) { + if (! file_exists($nonDirectory)) { return new self(sprintf('%s does not exists', $nonDirectory)); } + return new self(sprintf('%s is must to be a directory not a file', $nonDirectory)); } /** * @param mixed $nonStringValue + * * @return InvalidDirectory */ public static function fromNonStringValue($nonStringValue) { - $type = is_object($nonStringValue) ? get_class($nonStringValue) : gettype($nonStringValue) ; - return new self(sprintf('Expected string, %s given', $type)); + return new self(sprintf( + 'Expected string, %s given', + is_object($nonStringValue) ? get_class($nonStringValue) : gettype($nonStringValue) + )); } } From fc9a9120f86da19787c702ac7a7f5dfcac262b90 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:19:37 +0200 Subject: [PATCH 20/51] #214 CS (inlining expressions, docblock spacing) --- src/SourceLocator/Exception/InvalidFileInfo.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Exception/InvalidFileInfo.php b/src/SourceLocator/Exception/InvalidFileInfo.php index 2f977e1d8..441abe3f3 100644 --- a/src/SourceLocator/Exception/InvalidFileInfo.php +++ b/src/SourceLocator/Exception/InvalidFileInfo.php @@ -6,11 +6,14 @@ class InvalidFileInfo extends \RuntimeException { /** * @param mixed $nonSplFileInfo + * * @return InvalidFileInfo */ public static function fromNonSplFileInfo($nonSplFileInfo) { - $type = is_object($nonSplFileInfo) ? get_class($nonSplFileInfo) : gettype($nonSplFileInfo) ; - return new self(sprintf('Expected an iterator of SplFileInfo instances, %s given instead', $type)); + return new self(sprintf( + 'Expected an iterator of SplFileInfo instances, %s given instead', + is_object($nonSplFileInfo) ? get_class($nonSplFileInfo) : gettype($nonSplFileInfo) + )); } } From fd6a20140673c0ff1e8e0f9c93816ee74c359eef Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:20:34 +0200 Subject: [PATCH 21/51] #214 documenting thrown exception type that was missing --- .../Type/MultipleDirectoriesAggregateSourceLocator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index dee27e13d..c796d4bc0 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -6,6 +6,7 @@ use BetterReflection\Identifier\IdentifierType; use BetterReflection\Reflector\Reflector; use BetterReflection\SourceLocator\Exception\InvalidDirectory; +use BetterReflection\SourceLocator\Exception\InvalidFileInfo; /** * This source locator loads all php files in an entire directory or multiple directories. @@ -19,7 +20,9 @@ class MultipleDirectoriesAggregateSourceLocator implements SourceLocator /** * @param string[] $directories directories to scan + * * @throws InvalidDirectory + * @throws InvalidFileInfo */ public function __construct(array $directories) { From 4b7c2f96cc48dabab7121814b18449b055a60c7d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:22:02 +0200 Subject: [PATCH 22/51] #214 CS (spacing, imported symbols) --- ...MultipleDirectoriesAggregateSourceLocator.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index c796d4bc0..2745c3950 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -7,6 +7,7 @@ use BetterReflection\Reflector\Reflector; use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Exception\InvalidFileInfo; +use RecursiveDirectoryIterator; /** * This source locator loads all php files in an entire directory or multiple directories. @@ -27,21 +28,26 @@ class MultipleDirectoriesAggregateSourceLocator implements SourceLocator public function __construct(array $directories) { $sourceLocators = []; + foreach ($directories as $directory) { - if (!is_string($directory)) { + if (! is_string($directory)) { throw InvalidDirectory::fromNonStringValue($directory); } - if (!is_dir($directory)) { + + if (! is_dir($directory)) { throw InvalidDirectory::fromNonDirectory($directory); } - $recursiveDirectoryIterator = new \RecursiveDirectoryIterator( + + $recursiveDirectoryIterator = new RecursiveDirectoryIterator( $directory, - \RecursiveDirectoryIterator::SKIP_DOTS + RecursiveDirectoryIterator::SKIP_DOTS ); + $sourceLocators[] = new SingleDirectorySourceLocator( - new \RecursiveIteratorIterator($recursiveDirectoryIterator) + new RecursiveIteratorIterator($recursiveDirectoryIterator) ); } + $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); } From 022750c5577ffe7ceaaf474af3f6fdbe54a9c630 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:22:37 +0200 Subject: [PATCH 23/51] #214 inlining expression, removing unused variable --- .../Type/MultipleDirectoriesAggregateSourceLocator.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index 2745c3950..ac623c0b4 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -38,13 +38,11 @@ public function __construct(array $directories) throw InvalidDirectory::fromNonDirectory($directory); } - $recursiveDirectoryIterator = new RecursiveDirectoryIterator( - $directory, - RecursiveDirectoryIterator::SKIP_DOTS - ); - $sourceLocators[] = new SingleDirectorySourceLocator( - new RecursiveIteratorIterator($recursiveDirectoryIterator) + new RecursiveIteratorIterator(new RecursiveDirectoryIterator( + $directory, + RecursiveDirectoryIterator::SKIP_DOTS + )) ); } From c0ed688d0755b4fd052e9acd87a6849cb2f2138b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:23:57 +0200 Subject: [PATCH 24/51] #214 moved from iterative `AggregateSourceLocator` instantiation to functional approach --- ...tipleDirectoriesAggregateSourceLocator.php | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index ac623c0b4..788feb678 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -27,26 +27,25 @@ class MultipleDirectoriesAggregateSourceLocator implements SourceLocator */ public function __construct(array $directories) { - $sourceLocators = []; + $this->aggregateSourceLocator = new AggregateSourceLocator(array_map( + function ($directory) { + if (! is_string($directory)) { + throw InvalidDirectory::fromNonStringValue($directory); + } - foreach ($directories as $directory) { - if (! is_string($directory)) { - throw InvalidDirectory::fromNonStringValue($directory); - } + if (! is_dir($directory)) { + throw InvalidDirectory::fromNonDirectory($directory); + } - if (! is_dir($directory)) { - throw InvalidDirectory::fromNonDirectory($directory); - } - - $sourceLocators[] = new SingleDirectorySourceLocator( - new RecursiveIteratorIterator(new RecursiveDirectoryIterator( - $directory, - RecursiveDirectoryIterator::SKIP_DOTS - )) - ); - } - - $this->aggregateSourceLocator = new AggregateSourceLocator($sourceLocators); + return new SingleDirectorySourceLocator( + new RecursiveIteratorIterator(new RecursiveDirectoryIterator( + $directory, + RecursiveDirectoryIterator::SKIP_DOTS + )) + ); + }, + $directories + )); } /** From 33be1110eeb4be54cd3ab36922caed701d984250 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:26:10 +0200 Subject: [PATCH 25/51] #214 importing used symbols, reducing inlined expression size --- .../MultipleDirectoriesAggregateSourceLocator.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php index 788feb678..7515e37ed 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php @@ -8,6 +8,7 @@ use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Exception\InvalidFileInfo; use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; /** * This source locator loads all php files in an entire directory or multiple directories. @@ -37,12 +38,10 @@ function ($directory) { throw InvalidDirectory::fromNonDirectory($directory); } - return new SingleDirectorySourceLocator( - new RecursiveIteratorIterator(new RecursiveDirectoryIterator( - $directory, - RecursiveDirectoryIterator::SKIP_DOTS - )) - ); + return new SingleDirectorySourceLocator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator( + $directory, + RecursiveDirectoryIterator::SKIP_DOTS + ))); }, $directories )); From 6f0eea44d4cee6deba1d3eb40f4e1a0bb92ec319 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:28:30 +0200 Subject: [PATCH 26/51] #214 renamed `MultipleDirectoriesAggregateSourceLocator` to `DirectoriesSourceLocator` (much simpler) --- ...eSourceLocator.php => DirectoriesSourceLocator.php} | 2 +- ...ocatorTest.php => DirectoriesSourceLocatorTest.php} | 10 +++++----- ...est.php => InvalidDirectoriesSourceLocatorTest.php} | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) rename src/SourceLocator/Type/{MultipleDirectoriesAggregateSourceLocator.php => DirectoriesSourceLocator.php} (96%) rename test/unit/SourceLocator/Type/{MultipleDirectoriesAggregateSourceLocatorTest.php => DirectoriesSourceLocatorTest.php} (74%) rename test/unit/SourceLocator/Type/{InvalidDirectorySourceLocatorTest.php => InvalidDirectoriesSourceLocatorTest.php} (70%) diff --git a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php b/src/SourceLocator/Type/DirectoriesSourceLocator.php similarity index 96% rename from src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php rename to src/SourceLocator/Type/DirectoriesSourceLocator.php index 7515e37ed..1ada93372 100644 --- a/src/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocator.php +++ b/src/SourceLocator/Type/DirectoriesSourceLocator.php @@ -13,7 +13,7 @@ /** * This source locator loads all php files in an entire directory or multiple directories. */ -class MultipleDirectoriesAggregateSourceLocator implements SourceLocator +class DirectoriesSourceLocator implements SourceLocator { /** * @var AggregateSourceLocator diff --git a/test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php similarity index 74% rename from test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php rename to test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index 03859c332..43be3eef3 100644 --- a/test/unit/SourceLocator/Type/MultipleDirectoriesAggregateSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -3,12 +3,12 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; -use BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator; +use BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator + * @covers \BetterReflection\SourceLocator\Type\DirectoriesSourceLocator */ -class MultipleDirectoriesAggregateSourceLocatorTest extends \PHPUnit_Framework_TestCase +class DirectoriesSourceLocatorTest extends \PHPUnit_Framework_TestCase { /** * @var string[] @@ -16,7 +16,7 @@ class MultipleDirectoriesAggregateSourceLocatorTest extends \PHPUnit_Framework_T private $directoryToScan = []; /** - * @var MultipleDirectoriesAggregateSourceLocator + * @var DirectoriesSourceLocator */ private $sourceLocator; @@ -24,7 +24,7 @@ public function setUp() { $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssets'; $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssetsFoo'; - $this->sourceLocator = new MultipleDirectoriesAggregateSourceLocator($this->directoryToScan); + $this->sourceLocator = new DirectoriesSourceLocator($this->directoryToScan); } public function testScanDirectoryClasses() diff --git a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php similarity index 70% rename from test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php rename to test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php index 0632ab664..07e27b5e3 100644 --- a/test/unit/SourceLocator/Type/InvalidDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php @@ -3,12 +3,12 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\SourceLocator\Exception\InvalidDirectory; -use BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator; +use BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\MultipleDirectoriesAggregateSourceLocator + * @covers \BetterReflection\SourceLocator\Type\DirectoriesSourceLocator */ -class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase +class InvalidDirectoriesSourceLocatorTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -20,7 +20,7 @@ class InvalidDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase */ public function testDirectoryToScan() { - new MultipleDirectoriesAggregateSourceLocator([$this->directoryToScan, $this->directoryToScan]); + new DirectoriesSourceLocator([$this->directoryToScan, $this->directoryToScan]); } /** @@ -30,7 +30,7 @@ public function testDirectoryToScan() public function testInvalidDirectory(array $directories) { $this->expectException(InvalidDirectory::class); - new MultipleDirectoriesAggregateSourceLocator($directories); + new DirectoriesSourceLocator($directories); } public function invalidDirectoriesProvider() From 1bf935eb2cd3b0e93fdb9896fd3c6fb0d8d092f0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:30:53 +0200 Subject: [PATCH 27/51] #214 renamed `SingleDirectorySourceLocator` to `FileIteratorSourceLocator` --- src/SourceLocator/Type/DirectoriesSourceLocator.php | 2 +- ...SourceLocator.php => FileIteratorSourceLocator.php} | 5 +++-- ...catorTest.php => FileIteratorSourceLocatorTest.php} | 10 +++++----- 3 files changed, 9 insertions(+), 8 deletions(-) rename src/SourceLocator/Type/{SingleDirectorySourceLocator.php => FileIteratorSourceLocator.php} (92%) rename test/unit/SourceLocator/Type/{SingleDirectorySourceLocatorTest.php => FileIteratorSourceLocatorTest.php} (88%) diff --git a/src/SourceLocator/Type/DirectoriesSourceLocator.php b/src/SourceLocator/Type/DirectoriesSourceLocator.php index 1ada93372..498a952bf 100644 --- a/src/SourceLocator/Type/DirectoriesSourceLocator.php +++ b/src/SourceLocator/Type/DirectoriesSourceLocator.php @@ -38,7 +38,7 @@ function ($directory) { throw InvalidDirectory::fromNonDirectory($directory); } - return new SingleDirectorySourceLocator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator( + return new FileIteratorSourceLocator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator( $directory, RecursiveDirectoryIterator::SKIP_DOTS ))); diff --git a/src/SourceLocator/Type/SingleDirectorySourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php similarity index 92% rename from src/SourceLocator/Type/SingleDirectorySourceLocator.php rename to src/SourceLocator/Type/FileIteratorSourceLocator.php index b1556e925..fc94a990d 100644 --- a/src/SourceLocator/Type/SingleDirectorySourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -10,7 +10,7 @@ /** * This source locator loads all php files from \FileSystemIterator */ -class SingleDirectorySourceLocator implements SourceLocator +class FileIteratorSourceLocator implements SourceLocator { /** * @var AggregateSourceLocator|null @@ -23,7 +23,8 @@ class SingleDirectorySourceLocator implements SourceLocator private $fileSystemIterator; /** - * @param \Iterator $fileInfoIterator + * @param \Iterator|\SplFileInfo[] $fileInfoIterator note: only \SplFileInfo allowed in this iterator + * * @throws InvalidFileInfo In case of iterator not contains only SplFileInfo */ public function __construct(\Iterator $fileInfoIterator) diff --git a/test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php similarity index 88% rename from test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php rename to test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index b0e96a3ac..aa2d67b02 100644 --- a/test/unit/SourceLocator/Type/SingleDirectorySourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -3,13 +3,13 @@ namespace BetterReflectionTest\SourceLocator\Type; use BetterReflection\Reflector\ClassReflector; -use BetterReflection\SourceLocator\Type\SingleDirectorySourceLocator; +use BetterReflection\SourceLocator\Type\FileIteratorSourceLocator; use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; /** - * @covers \BetterReflection\SourceLocator\Type\SingleDirectorySourceLocator + * @covers \BetterReflection\SourceLocator\Type\FileIteratorSourceLocator */ -class SingleDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase +class FileIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase { /** * @var string @@ -17,7 +17,7 @@ class SingleDirectorySourceLocatorTest extends \PHPUnit_Framework_TestCase private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; /** - * @var SingleDirectorySourceLocator + * @var FileIteratorSourceLocator */ private $sourceLocator; @@ -27,7 +27,7 @@ public function setUp() $this->directoryToScan, \RecursiveDirectoryIterator::SKIP_DOTS ); - $this->sourceLocator = new SingleDirectorySourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); + $this->sourceLocator = new FileIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); } public function testScanDirectoryClasses() From 89028def217a60392394ab3a5fa240c109710094 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:44:19 +0200 Subject: [PATCH 28/51] #214 moved from iterative to declarative locator construction --- .../Type/FileIteratorSourceLocator.php | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index fc94a990d..fd7e6db9b 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -18,7 +18,7 @@ class FileIteratorSourceLocator implements SourceLocator private $aggregateSourceLocator; /** - * @var \Iterator + * @var \Iterator|\SplFileInfo[] */ private $fileSystemIterator; @@ -30,10 +30,11 @@ class FileIteratorSourceLocator implements SourceLocator public function __construct(\Iterator $fileInfoIterator) { foreach ($fileInfoIterator as $fileInfo) { - if (!$fileInfo instanceof \SplFileInfo) { + if (! $fileInfo instanceof \SplFileInfo) { throw InvalidFileInfo::fromNonSplFileInfo($fileInfo); } } + $this->fileSystemIterator = $fileInfoIterator; } @@ -53,14 +54,16 @@ private function getAggregatedSourceLocator() */ private function scan() { - $sourceLocators = []; - foreach ($this->fileSystemIterator as $item) { - /* @var $item \SplFileInfo */ - if ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php') { - $sourceLocators[] = new SingleFileSourceLocator($item->getRealPath()); - } - } - return $sourceLocators; + return array_filter(array_map( + function (\SplFileInfo $item) { + if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php')) { + return; + } + + return new SingleFileSourceLocator($item->getRealPath()); + }, + iterator_to_array($this->fileSystemIterator) + )); } /** From 7316679e4c260a987016b01d384d0bfd25c11451 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:48:15 +0200 Subject: [PATCH 29/51] #214 file iterator values should be packed --- src/SourceLocator/Type/FileIteratorSourceLocator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index fd7e6db9b..9b59edc42 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -54,7 +54,7 @@ private function getAggregatedSourceLocator() */ private function scan() { - return array_filter(array_map( + return array_values(array_filter(array_map( function (\SplFileInfo $item) { if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php')) { return; @@ -63,7 +63,7 @@ function (\SplFileInfo $item) { return new SingleFileSourceLocator($item->getRealPath()); }, iterator_to_array($this->fileSystemIterator) - )); + ))); } /** From ee21cc69f76fdc6a017d05779b1a5668922fd74d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 16:48:43 +0200 Subject: [PATCH 30/51] #214 `AggregateSourceLocator` expects a packed array of values --- src/SourceLocator/Type/DirectoriesSourceLocator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/DirectoriesSourceLocator.php b/src/SourceLocator/Type/DirectoriesSourceLocator.php index 498a952bf..d508f8a86 100644 --- a/src/SourceLocator/Type/DirectoriesSourceLocator.php +++ b/src/SourceLocator/Type/DirectoriesSourceLocator.php @@ -28,7 +28,7 @@ class DirectoriesSourceLocator implements SourceLocator */ public function __construct(array $directories) { - $this->aggregateSourceLocator = new AggregateSourceLocator(array_map( + $this->aggregateSourceLocator = new AggregateSourceLocator(array_values(array_map( function ($directory) { if (! is_string($directory)) { throw InvalidDirectory::fromNonStringValue($directory); @@ -44,7 +44,7 @@ function ($directory) { ))); }, $directories - )); + ))); } /** From 981731b04bc3af239721646ad96082ebbe28585e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:11:23 +0200 Subject: [PATCH 31/51] #214 refactoring `InvalidDirectoryTest` to use a data provider --- .../Exception/InvalidDirectoryTest.php | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php index 29782055b..e73c561c2 100644 --- a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php +++ b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php @@ -9,6 +9,37 @@ */ class InvalidDirectoryTest extends \PHPUnit_Framework_TestCase { + /** + * @dataProvider nonStringValuesProvider + * + * @param string $expectedMessage + * @param mixed $value + * + * @return void + */ + public function testFromNonStringValue($expectedMessage, $value) + { + $exception = InvalidDirectory::fromNonStringValue($value); + + self::assertInstanceOf(InvalidDirectory::class, $exception); + self::assertSame($expectedMessage, $exception->getMessage()); + } + + /** + * @return string[][]|mixed[][] + */ + public function nonStringValuesProvider() + { + return [ + ['Expected string, stdClass given', new \stdClass()], + ['Expected string, boolean given', true], + ['Expected string, NULL given', null], + ['Expected string, integer given', 100], + ['Expected string, double given', 100.35], + ['Expected string, array given', []], + ]; + } + public function testExceptionMessage() { $e = InvalidDirectory::fromNonDirectory('testDir'); From c55226032c15bbac58d0b6e667f657858b956dfb Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:16:18 +0200 Subject: [PATCH 32/51] #214 splitting out tests for `InvalidDirectory` involving patch existence checks --- .../Exception/InvalidDirectory.php | 4 ++-- .../Exception/InvalidDirectoryTest.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 064add0f1..5b020c765 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -12,10 +12,10 @@ class InvalidDirectory extends \RuntimeException public static function fromNonDirectory($nonDirectory) { if (! file_exists($nonDirectory)) { - return new self(sprintf('%s does not exists', $nonDirectory)); + return new self(sprintf('"%s" does not exists', $nonDirectory)); } - return new self(sprintf('%s is must to be a directory not a file', $nonDirectory)); + return new self(sprintf('"%s" is must to be a directory not a file', $nonDirectory)); } /** diff --git a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php index e73c561c2..399f0603f 100644 --- a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php +++ b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php @@ -40,6 +40,23 @@ public function nonStringValuesProvider() ]; } + public function testFromNonDirectoryWithNonExistingPath() + { + $directory = uniqid(sys_get_temp_dir() . 'non-existing', true); + $exception = InvalidDirectory::fromNonDirectory($directory); + + self::assertInstanceOf(InvalidDirectory::class, $exception); + self::assertSame(sprintf('"%s" does not exists', $directory), $exception->getMessage()); + } + + public function testFromNonDirectoryWithFile() + { + $exception = InvalidDirectory::fromNonDirectory(__FILE__); + + self::assertInstanceOf(InvalidDirectory::class, $exception); + self::assertSame(sprintf('"%s" must be a directory, not a file', __FILE__), $exception->getMessage()); + } + public function testExceptionMessage() { $e = InvalidDirectory::fromNonDirectory('testDir'); From da46da632a94ca20b4c67a88ed9bf6f105ba5802 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:16:36 +0200 Subject: [PATCH 33/51] #214 removing duplicate `InvalidDirectory` tests --- .../Exception/InvalidDirectoryTest.php | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php index 399f0603f..26d65a69e 100644 --- a/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php +++ b/test/unit/SourceLocator/Exception/InvalidDirectoryTest.php @@ -56,37 +56,4 @@ public function testFromNonDirectoryWithFile() self::assertInstanceOf(InvalidDirectory::class, $exception); self::assertSame(sprintf('"%s" must be a directory, not a file', __FILE__), $exception->getMessage()); } - - public function testExceptionMessage() - { - $e = InvalidDirectory::fromNonDirectory('testDir'); - $this->assertEquals(sprintf('%s does not exists', 'testDir'), $e->getMessage()); - - $e = InvalidDirectory::fromNonDirectory(__FILE__); - $this->assertEquals(sprintf('%s is must to be a directory not a file', __FILE__), $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(new \stdClass()); - $expected = 'Expected string, stdClass given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(true); - $expected = 'Expected string, boolean given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(null); - $expected = 'Expected string, NULL given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100); - $expected = 'Expected string, integer given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue(100.35); - $expected = 'Expected string, double given'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidDirectory::fromNonStringValue([100, 200]); - $expected = 'Expected string, array given'; - $this->assertEquals($expected, $e->getMessage()); - } } From 8b975f51df29dec4736acd037bf98b667f172b7a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:21:36 +0200 Subject: [PATCH 34/51] #214 corrected `InvalidDirectory` message grammar --- src/SourceLocator/Exception/InvalidDirectory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SourceLocator/Exception/InvalidDirectory.php b/src/SourceLocator/Exception/InvalidDirectory.php index 5b020c765..dc0fd6e85 100644 --- a/src/SourceLocator/Exception/InvalidDirectory.php +++ b/src/SourceLocator/Exception/InvalidDirectory.php @@ -15,7 +15,7 @@ public static function fromNonDirectory($nonDirectory) return new self(sprintf('"%s" does not exists', $nonDirectory)); } - return new self(sprintf('"%s" is must to be a directory not a file', $nonDirectory)); + return new self(sprintf('"%s" must be a directory, not a file', $nonDirectory)); } /** From 2f27f3754c6c89aed34351f21a26bc2b60e578e6 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:21:51 +0200 Subject: [PATCH 35/51] #214 refactored `InvalidFileInfoTest` via data providers --- .../Exception/InvalidFileInfoTest.php | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php b/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php index 9477f933b..db1265f78 100644 --- a/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php +++ b/test/unit/SourceLocator/Exception/InvalidFileInfoTest.php @@ -9,30 +9,34 @@ */ class InvalidFileInfoTest extends \PHPUnit_Framework_TestCase { - public function testExceptionMessage() + /** + * @dataProvider nonSplFileInfoProvider + * + * @param string $expectedMessage + * @param mixed $value + * + * @return void + */ + public function testFromNonSplFileInfo($expectedMessage, $value) { - $e = InvalidFileInfo::fromNonSplFileInfo(new \stdClass()); - $expected = 'Expected an iterator of SplFileInfo instances, stdClass given instead'; - $this->assertEquals($expected, $e->getMessage()); + $exception = InvalidFileInfo::fromNonSplFileInfo($value); - $e = InvalidFileInfo::fromNonSplFileInfo(true); - $expected = 'Expected an iterator of SplFileInfo instances, boolean given instead'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidFileInfo::fromNonSplFileInfo(null); - $expected = 'Expected an iterator of SplFileInfo instances, NULL given instead'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidFileInfo::fromNonSplFileInfo(100); - $expected = 'Expected an iterator of SplFileInfo instances, integer given instead'; - $this->assertEquals($expected, $e->getMessage()); - - $e = InvalidFileInfo::fromNonSplFileInfo(100.35); - $expected = 'Expected an iterator of SplFileInfo instances, double given instead'; - $this->assertEquals($expected, $e->getMessage()); + self::assertInstanceOf(InvalidFileInfo::class, $exception); + self::assertSame($expectedMessage, $exception->getMessage()); + } - $e = InvalidFileInfo::fromNonSplFileInfo([100, 200]); - $expected = 'Expected an iterator of SplFileInfo instances, array given instead'; - $this->assertEquals($expected, $e->getMessage()); + /** + * @return string[][]|mixed[][] + */ + public function nonSplFileInfoProvider() + { + return [ + ['Expected an iterator of SplFileInfo instances, stdClass given instead', new \stdClass()], + ['Expected an iterator of SplFileInfo instances, boolean given instead', true], + ['Expected an iterator of SplFileInfo instances, NULL given instead', null], + ['Expected an iterator of SplFileInfo instances, integer given instead', 100], + ['Expected an iterator of SplFileInfo instances, double given instead', 100.35], + ['Expected an iterator of SplFileInfo instances, array given instead', []], + ]; } } From fbafbb5dbe67e204c71e663e61c931ead4cb72a6 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:22:46 +0200 Subject: [PATCH 36/51] #214 removed unused variable --- .../Type/DirectoriesSourceLocatorTest.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index 43be3eef3..8db848b68 100644 --- a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -10,11 +10,6 @@ */ class DirectoriesSourceLocatorTest extends \PHPUnit_Framework_TestCase { - /** - * @var string[] - */ - private $directoryToScan = []; - /** * @var DirectoriesSourceLocator */ @@ -22,9 +17,10 @@ class DirectoriesSourceLocatorTest extends \PHPUnit_Framework_TestCase public function setUp() { - $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssets'; - $this->directoryToScan[] = __DIR__ . '/../../Assets/DirectoryScannerAssetsFoo'; - $this->sourceLocator = new DirectoriesSourceLocator($this->directoryToScan); + $this->sourceLocator = new DirectoriesSourceLocator([ + __DIR__ . '/../../Assets/DirectoryScannerAssets', + __DIR__ . '/../../Assets/DirectoryScannerAssetsFoo', + ]); } public function testScanDirectoryClasses() From 2e0e9a86862c0631223ca92f2a2806fd52fad83a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:28:14 +0200 Subject: [PATCH 37/51] #214 refactored `DirectoriesSourceLocatorTest` to not rely on indirect calls from `ClassReflector`, imported used symbols --- .../Type/DirectoriesSourceLocatorTest.php | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index 8db848b68..3cad1c4d1 100644 --- a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -2,8 +2,12 @@ namespace BetterReflectionTest\SourceLocator\Type; +use BetterReflection\Identifier\IdentifierType; +use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; +use BetterReflectionTest\Assets\DirectoryScannerAssets; +use BetterReflectionTest\Assets\DirectoryScannerAssetsFoo; /** * @covers \BetterReflection\SourceLocator\Type\DirectoriesSourceLocator @@ -25,17 +29,25 @@ public function setUp() public function testScanDirectoryClasses() { - $reflector = new ClassReflector($this->sourceLocator); - $classes = $reflector->getAllClasses(); + $classes = $this->sourceLocator->locateIdentifiersByType( + new ClassReflector($this->sourceLocator), + new IdentifierType(IdentifierType::IDENTIFIER_CLASS) + ); + $this->assertCount(4, $classes); - $classNames = []; - foreach ($classes as $class) { - $classNames[] = $class->getName(); - } + + $classNames = array_map( + function (ReflectionClass $reflectionClass) { + return $reflectionClass->getName(); + }, + $classes + ); + sort($classNames); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssetsFoo\Bar\FooBar', $classNames[0]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssetsFoo\Foo', $classNames[1]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[2]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[3]); + + $this->assertEquals(DirectoryScannerAssetsFoo\Bar\FooBar::class, $classNames[0]); + $this->assertEquals(DirectoryScannerAssetsFoo\Foo::class, $classNames[1]); + $this->assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[2]); + $this->assertEquals(DirectoryScannerAssets\Foo::class, $classNames[3]); } } From 2a8377ef36429c9096275c194a6d2177c4977f3d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:28:36 +0200 Subject: [PATCH 38/51] #214 s/$this->assert/self::assert --- .../Type/DirectoriesSourceLocatorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index 3cad1c4d1..65b8b29b7 100644 --- a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -34,7 +34,7 @@ public function testScanDirectoryClasses() new IdentifierType(IdentifierType::IDENTIFIER_CLASS) ); - $this->assertCount(4, $classes); + self::assertCount(4, $classes); $classNames = array_map( function (ReflectionClass $reflectionClass) { @@ -45,9 +45,9 @@ function (ReflectionClass $reflectionClass) { sort($classNames); - $this->assertEquals(DirectoryScannerAssetsFoo\Bar\FooBar::class, $classNames[0]); - $this->assertEquals(DirectoryScannerAssetsFoo\Foo::class, $classNames[1]); - $this->assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[2]); - $this->assertEquals(DirectoryScannerAssets\Foo::class, $classNames[3]); + self::assertEquals(DirectoryScannerAssetsFoo\Bar\FooBar::class, $classNames[0]); + self::assertEquals(DirectoryScannerAssetsFoo\Foo::class, $classNames[1]); + self::assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[2]); + self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[3]); } } From cf4ce8bc8ead7eb85eb202c4262d79d1014a7ac1 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:29:03 +0200 Subject: [PATCH 39/51] #214 s/$this->assert/self::assert --- .../Type/FileIteratorSourceLocatorTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index aa2d67b02..ef1bfe72a 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -34,14 +34,14 @@ public function testScanDirectoryClasses() { $reflector = new ClassReflector($this->sourceLocator); $classes = $reflector->getAllClasses(); - $this->assertCount(2, $classes); + self::assertCount(2, $classes); $classNames = []; foreach ($classes as $class) { $classNames[] = $class->getName(); } sort($classNames); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); } public function testScanDirectoryFiles() @@ -51,7 +51,7 @@ public function testScanDirectoryFiles() $method = $class->getMethod('scan'); $method->setAccessible(true); $result = $method->invoke($fileSystemIteratorSourceLocator); - $this->assertCount(3, $result); + self::assertCount(3, $result); // test file path $files = []; @@ -62,9 +62,9 @@ public function testScanDirectoryFiles() $files[] = realpath($property->getValue($file)); } sort($files); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); - $this->assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); + self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); + self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); + self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); // test class names $classNames = []; @@ -76,8 +76,8 @@ public function testScanDirectoryFiles() } } sort($classNames); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - $this->assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); - $this->assertCount(2, $classNames); + self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); + self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + self::assertCount(2, $classNames); } } From 241e0899cfa3def23a1d49eb5b7845ac2b64530b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:29:29 +0200 Subject: [PATCH 40/51] #214 removed unused variable --- .../SourceLocator/Type/FileIteratorSourceLocatorTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index ef1bfe72a..d1ddc9fe0 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -11,11 +11,6 @@ */ class FileIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase { - /** - * @var string - */ - private $directoryToScan = __DIR__ . '/../../Assets/DirectoryScannerAssets'; - /** * @var FileIteratorSourceLocator */ @@ -24,7 +19,7 @@ class FileIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase public function setUp() { $fileSystemIterator = new \RecursiveDirectoryIterator( - $this->directoryToScan, + __DIR__ . '/../../Assets/DirectoryScannerAssets', \RecursiveDirectoryIterator::SKIP_DOTS ); $this->sourceLocator = new FileIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); From e61d3fe1d95f850db4a6da3d3d22b22ecad1d887 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:30:25 +0200 Subject: [PATCH 41/51] #214 inlining useless assignments --- .../Type/FileIteratorSourceLocatorTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index d1ddc9fe0..fa19cb020 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -5,6 +5,8 @@ use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Type\FileIteratorSourceLocator; use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; /** * @covers \BetterReflection\SourceLocator\Type\FileIteratorSourceLocator @@ -16,13 +18,17 @@ class FileIteratorSourceLocatorTest extends \PHPUnit_Framework_TestCase */ private $sourceLocator; + /** + * {@inheritDoc} + */ public function setUp() { - $fileSystemIterator = new \RecursiveDirectoryIterator( - __DIR__ . '/../../Assets/DirectoryScannerAssets', - \RecursiveDirectoryIterator::SKIP_DOTS + $this->sourceLocator = new FileIteratorSourceLocator( + new RecursiveIteratorIterator(new RecursiveDirectoryIterator( + __DIR__ . '/../../Assets/DirectoryScannerAssets', + RecursiveDirectoryIterator::SKIP_DOTS + )) ); - $this->sourceLocator = new FileIteratorSourceLocator(new \RecursiveIteratorIterator($fileSystemIterator)); } public function testScanDirectoryClasses() From 8a54771030300bf8b2d374f3e317004dbd2ef440 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:33:10 +0200 Subject: [PATCH 42/51] #214 refactored `FileIteratorSourceLocatorTest` to not rely on indirect calls from `ClassReflector`, imported used symbols --- .../Type/FileIteratorSourceLocatorTest.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index fa19cb020..c149cc32e 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -2,9 +2,12 @@ namespace BetterReflectionTest\SourceLocator\Type; +use BetterReflection\Identifier\IdentifierType; +use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Type\FileIteratorSourceLocator; use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; +use BetterReflectionTest\Assets\DirectoryScannerAssets; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; @@ -33,16 +36,24 @@ public function setUp() public function testScanDirectoryClasses() { - $reflector = new ClassReflector($this->sourceLocator); - $classes = $reflector->getAllClasses(); + $classes = $this->sourceLocator->locateIdentifiersByType( + new ClassReflector($this->sourceLocator), + new IdentifierType(IdentifierType::IDENTIFIER_CLASS) + ); + self::assertCount(2, $classes); - $classNames = []; - foreach ($classes as $class) { - $classNames[] = $class->getName(); - } + + $classNames = array_map( + function (ReflectionClass $reflectionClass) { + return $reflectionClass->getName(); + }, + $classes + ); + sort($classNames); - self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); + + self::assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[0]); + self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[1]); } public function testScanDirectoryFiles() From 0b3429ef15cf558a990baebb8a74a03360b0e5f3 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:33:28 +0200 Subject: [PATCH 43/51] #214 removed private API tests (very risky!) --- .../Type/FileIteratorSourceLocatorTest.php | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index c149cc32e..8a610a7e9 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -55,41 +55,4 @@ function (ReflectionClass $reflectionClass) { self::assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[0]); self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[1]); } - - public function testScanDirectoryFiles() - { - $fileSystemIteratorSourceLocator = $this->sourceLocator; - $class = new \ReflectionClass(get_class($fileSystemIteratorSourceLocator)); - $method = $class->getMethod('scan'); - $method->setAccessible(true); - $result = $method->invoke($fileSystemIteratorSourceLocator); - self::assertCount(3, $result); - - // test file path - $files = []; - foreach ($result as $file) { - $class = new \ReflectionClass('BetterReflection\SourceLocator\Type\SingleFileSourceLocator'); - $property = $class->getProperty('filename'); - $property->setAccessible(true); - $files[] = realpath($property->getValue($file)); - } - sort($files); - self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/Empty.php'), $files[0]); - self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Bar/FooBar.php'), $files[1]); - self::assertEquals(realpath(__DIR__ . '/../../Assets/DirectoryScannerAssets/Foo.php'), $files[2]); - - // test class names - $classNames = []; - foreach ($result as $file) { - /* @var $file SingleFileSourceLocator */ - $reflector = new ClassReflector($file); - foreach ($reflector->getAllClasses() as $class) { - $classNames[] = $class->getName(); - } - } - sort($classNames); - self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Bar\FooBar', $classNames[0]); - self::assertEquals('BetterReflectionTest\Assets\DirectoryScannerAssets\Foo', $classNames[1]); - self::assertCount(2, $classNames); - } } From b99e342402fe4c57e521dcb52b7206f7e03ac260 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:33:42 +0200 Subject: [PATCH 44/51] #214 optimized use statement --- test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index 8a610a7e9..29aa17456 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -6,7 +6,6 @@ use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; use BetterReflection\SourceLocator\Type\FileIteratorSourceLocator; -use BetterReflection\SourceLocator\Type\SingleFileSourceLocator; use BetterReflectionTest\Assets\DirectoryScannerAssets; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; From 2d5867be7b3fc113d491c6efedd4d15b49892e6e Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:38:10 +0200 Subject: [PATCH 45/51] #214 moved invalid scenarios into the `DirectoriesSourceLocatorTest` --- .../Type/DirectoriesSourceLocatorTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index 65b8b29b7..bb2406102 100644 --- a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -5,6 +5,7 @@ use BetterReflection\Identifier\IdentifierType; use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; +use BetterReflection\SourceLocator\Exception\InvalidDirectory; use BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; use BetterReflectionTest\Assets\DirectoryScannerAssets; use BetterReflectionTest\Assets\DirectoryScannerAssetsFoo; @@ -50,4 +51,38 @@ function (ReflectionClass $reflectionClass) { self::assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[2]); self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[3]); } + + /** + * @dataProvider invalidDirectoriesProvider + * + * @param array $directories + */ + public function testInvalidDirectory(array $directories) + { + $this->expectException(InvalidDirectory::class); + + new DirectoriesSourceLocator($directories); + } + + public function invalidDirectoriesProvider() + { + $validDir = __DIR__ . '/../../Assets/DirectoryScannerAssets'; + + return [ + [[__DIR__ . '/' . uniqid('nonExisting', true)]], + [[__FILE__]], + [[1]], + [[1.23]], + [[true]], + [[new \stdClass()]], + [[null]], + [[$validDir, __DIR__ . '/' . uniqid('nonExisting', true)]], + [[$validDir, __FILE__]], + [[$validDir, 1]], + [[$validDir, 1.23]], + [[$validDir, true]], + [[$validDir, new \stdClass()]], + [[$validDir, null]], + ]; + } } From 1fa3c1b41878b8c815cabfc3a2ce9387b07666c4 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:40:30 +0200 Subject: [PATCH 46/51] #214 removing redundant `InvalidDirectoriesSourceLocatorTest` --- .../InvalidDirectoriesSourceLocatorTest.php | 46 ------------------- 1 file changed, 46 deletions(-) delete mode 100644 test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php diff --git a/test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php deleted file mode 100644 index 07e27b5e3..000000000 --- a/test/unit/SourceLocator/Type/InvalidDirectoriesSourceLocatorTest.php +++ /dev/null @@ -1,46 +0,0 @@ -directoryToScan, $this->directoryToScan]); - } - - /** - * @dataProvider invalidDirectoriesProvider - * @param array $directories - */ - public function testInvalidDirectory(array $directories) - { - $this->expectException(InvalidDirectory::class); - new DirectoriesSourceLocator($directories); - } - - public function invalidDirectoriesProvider() - { - return [ - [[$this->directoryToScan, substr($this->directoryToScan, 0, strlen($this->directoryToScan)-1)]], - [[$this->directoryToScan, 1]], - [[$this->directoryToScan, true]], - [[$this->directoryToScan, new \stdClass()]], - [[$this->directoryToScan, null]], - ]; - } -} From 4aefc527d48689894dbf8328a4747b96141575f8 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:42:23 +0200 Subject: [PATCH 47/51] #214 aligning class names in the documentation with the actual behavior and current class names --- docs/usage.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 9315993d8..06e2e7dac 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -94,11 +94,11 @@ within the `Reflector`s. The library comes bundled with the following * `AggregateSourceLocator` - a combination of multiple `SourceLocator`s which are hunted through in the given order to locate the source. - * `SingleDirectorySourceLocator` - iterates all files in a single directory - (and descends into child directories) + * `FileIteratorSourceLocator` - iterates all files in a given iterator + containing `SplFileInfo` instances. - * `MultipleDirectoriesAggregateSourceLocator` - iterates a list of directories (and their children), - basically an aggregate version of `SingleDirectorySourceLocator` + * `DirectoriesSourceLocator` - iterates over all `.php` files in a list of + directories, and all their descendants. A `SourceLocator` is a callable, which when invoked must be given an `Identifier` (which describes a class/function/etc.). The `SourceLocator` From 980e45d4eeb0458b7fa9e7c6020a30223c58e31b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 17:44:07 +0200 Subject: [PATCH 48/51] #214 inlining `scan()` behavior --- .../Type/FileIteratorSourceLocator.php | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index 9b59edc42..a15ed48c7 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -39,31 +39,21 @@ public function __construct(\Iterator $fileInfoIterator) } /** - * Get a AggregateSourceLocator, create it if null. * @return AggregateSourceLocator */ private function getAggregatedSourceLocator() { return $this->aggregateSourceLocator ? - $this->aggregateSourceLocator : new AggregateSourceLocator($this->scan()); - } - - /** - * scan target directory and resulted as SourceLocator[] - * @return SourceLocator[] - */ - private function scan() - { - return array_values(array_filter(array_map( - function (\SplFileInfo $item) { - if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php')) { - return; - } + $this->aggregateSourceLocator : new AggregateSourceLocator(array_values(array_filter(array_map( + function (\SplFileInfo $item) { + if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php')) { + return; + } - return new SingleFileSourceLocator($item->getRealPath()); - }, - iterator_to_array($this->fileSystemIterator) - ))); + return new SingleFileSourceLocator($item->getRealPath()); + }, + iterator_to_array($this->fileSystemIterator) + )))); } /** From b748ee580c27ed9faf0194920ddd57810837b341 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 18:03:34 +0200 Subject: [PATCH 49/51] #214 covering `FileIteratorSourceLocator#locateIdentifier()` logic --- .../Type/FileIteratorSourceLocatorTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php index 29aa17456..460e444d6 100644 --- a/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/FileIteratorSourceLocatorTest.php @@ -2,6 +2,7 @@ namespace BetterReflectionTest\SourceLocator\Type; +use BetterReflection\Identifier\Identifier; use BetterReflection\Identifier\IdentifierType; use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; @@ -54,4 +55,18 @@ function (ReflectionClass $reflectionClass) { self::assertEquals(DirectoryScannerAssets\Bar\FooBar::class, $classNames[0]); self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[1]); } + + public function testLocateIdentifier() + { + $class = $this->sourceLocator->locateIdentifier( + new ClassReflector($this->sourceLocator), + new Identifier( + DirectoryScannerAssets\Bar\FooBar::class, + new IdentifierType(IdentifierType::IDENTIFIER_CLASS) + ) + ); + + self::assertInstanceOf(ReflectionClass::class, $class); + self::assertSame(DirectoryScannerAssets\Bar\FooBar::class, $class->getName()); + } } From ed8360104998052ba18f2f1302aa5824a0e5c83a Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 18:04:18 +0200 Subject: [PATCH 50/51] #214 covering `DirectoriesSourceLocator#locateIdentifier()` logic --- .../Type/DirectoriesSourceLocatorTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php index bb2406102..6bae739ea 100644 --- a/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php +++ b/test/unit/SourceLocator/Type/DirectoriesSourceLocatorTest.php @@ -2,6 +2,7 @@ namespace BetterReflectionTest\SourceLocator\Type; +use BetterReflection\Identifier\Identifier; use BetterReflection\Identifier\IdentifierType; use BetterReflection\Reflection\ReflectionClass; use BetterReflection\Reflector\ClassReflector; @@ -52,6 +53,20 @@ function (ReflectionClass $reflectionClass) { self::assertEquals(DirectoryScannerAssets\Foo::class, $classNames[3]); } + public function testLocateIdentifier() + { + $class = $this->sourceLocator->locateIdentifier( + new ClassReflector($this->sourceLocator), + new Identifier( + DirectoryScannerAssets\Bar\FooBar::class, + new IdentifierType(IdentifierType::IDENTIFIER_CLASS) + ) + ); + + self::assertInstanceOf(ReflectionClass::class, $class); + self::assertSame(DirectoryScannerAssets\Bar\FooBar::class, $class->getName()); + } + /** * @dataProvider invalidDirectoriesProvider * From 9a40fa2acc4d9e8cb420d9d982ba9c27363005f6 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Sep 2016 18:06:19 +0200 Subject: [PATCH 51/51] #214 explicit `null` return, strict comparison --- src/SourceLocator/Type/FileIteratorSourceLocator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SourceLocator/Type/FileIteratorSourceLocator.php b/src/SourceLocator/Type/FileIteratorSourceLocator.php index a15ed48c7..63824a4e4 100644 --- a/src/SourceLocator/Type/FileIteratorSourceLocator.php +++ b/src/SourceLocator/Type/FileIteratorSourceLocator.php @@ -46,8 +46,8 @@ private function getAggregatedSourceLocator() return $this->aggregateSourceLocator ? $this->aggregateSourceLocator : new AggregateSourceLocator(array_values(array_filter(array_map( function (\SplFileInfo $item) { - if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) == 'php')) { - return; + if (! ($item->isFile() && pathinfo($item->getRealPath(), \PATHINFO_EXTENSION) === 'php')) { + return null; } return new SingleFileSourceLocator($item->getRealPath());