From 841294cd5307288bd617cd0544a07124280c35f1 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Sun, 15 Sep 2019 14:28:01 +0000 Subject: [PATCH] Add ClassFinder::namespaceHasClasses instead of throwing on empty namespaces --- README.md | 1 - ci/php53.sh | 5 ++- ci/php72.sh | 5 ++- docs/changelog.md | 8 ++++- docs/exceptions/unknownNamespace.md | 2 ++ src/ClassFinder.php | 51 +++++++++++++++++++++-------- src/FinderInterface.php | 4 +-- test/app1/src/ClassFinderTest.php | 11 +++---- test/app1/src/FilesTest.php | 9 ++--- test/app1/src/PSR4Test.php | 11 +++---- 10 files changed, 67 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 34acf93..f9ac5b6 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,6 @@ Documentation * [Files could not locate PHP](docs/exceptions/filesCouldNotLocatePHP.md) * [Files exec not available](docs/exceptions/filesExecNotAvailable.md) * [Missing composer.json](docs/exceptions/missingComposerConfig.md) -* [Unknown namespace](docs/exceptions/unknownNamespace.md) **Internals** diff --git a/ci/php53.sh b/ci/php53.sh index f23b241..e644fe2 100755 --- a/ci/php53.sh +++ b/ci/php53.sh @@ -1,7 +1,10 @@ +#!/usr/bin/env bash +set -Eeuxo pipefail + composer install --working-dir=$1/test/app1 --quiet || exit 1 composer install --working-dir=$1/test/app2 --quiet || exit 1 composer install --working-dir=$1 --quiet || exit 1 php --version php $1/vendor/bin/phpunit --testsuite all php $1/vendor/bin/phpunit --testsuite noAutoload -php -d disable_functions=exec ./vendor/phpunit/phpunit/phpunit ./test/app1/src/ClassFinderTest.php --filter=testWorksWhenExecIsDisabled \ No newline at end of file +php -d disable_functions=exec ./vendor/phpunit/phpunit/phpunit ./test/app1/src/ClassFinderTest.php --filter=testWorksWhenExecIsDisabled diff --git a/ci/php72.sh b/ci/php72.sh index f23b241..e644fe2 100755 --- a/ci/php72.sh +++ b/ci/php72.sh @@ -1,7 +1,10 @@ +#!/usr/bin/env bash +set -Eeuxo pipefail + composer install --working-dir=$1/test/app1 --quiet || exit 1 composer install --working-dir=$1/test/app2 --quiet || exit 1 composer install --working-dir=$1 --quiet || exit 1 php --version php $1/vendor/bin/phpunit --testsuite all php $1/vendor/bin/phpunit --testsuite noAutoload -php -d disable_functions=exec ./vendor/phpunit/phpunit/phpunit ./test/app1/src/ClassFinderTest.php --filter=testWorksWhenExecIsDisabled \ No newline at end of file +php -d disable_functions=exec ./vendor/phpunit/phpunit/phpunit ./test/app1/src/ClassFinderTest.php --filter=testWorksWhenExecIsDisabled diff --git a/docs/changelog.md b/docs/changelog.md index 409820e..cffeb2e 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,3 +1,9 @@ +Unreleased +---------- + +* Don't throw if a namespace contains no classes +* Add method `Classfinder::namespaceHasClasses` to detect if a namespace is empty + Version 0.3.3 ------------- @@ -139,4 +145,4 @@ ClassFinder::appRoot = '/home/hpierce/whatevs'; New overriding app root: ``` ClassFinder::setAppRoot('/home/hpierce/whatevs'); -``` \ No newline at end of file +``` diff --git a/docs/exceptions/unknownNamespace.md b/docs/exceptions/unknownNamespace.md index be55bcd..c7e62a4 100644 --- a/docs/exceptions/unknownNamespace.md +++ b/docs/exceptions/unknownNamespace.md @@ -1,6 +1,8 @@ Unknown Namespace ----------------- +__This exception only occurs in versions 0.3.x and lower.__ + Example PHP: ``` isNamespaceKnown($namespace); - }); - - if (count($findersWithNamespace) === 0) { - throw new ClassFinderException(sprintf("Unknown namespace '%s'. See '%s' for details.", - $namespace, - 'https://gitlab.com/hpierce1102/ClassFinder/blob/master/docs/exceptions/unknownNamespace.md' - )); - } + $findersWithNamespace = self::findersWithNamespace($namespace); $classes = array_reduce($findersWithNamespace, function($carry, FinderInterface $finder) use ($namespace, $options){ return array_merge($carry, $finder->findClasses($namespace, $options)); @@ -85,6 +80,20 @@ class ClassFinder return array_unique($classes); } + /** + * Check if a given namespace contains any classes. + * + * @param string $namespace + * + * @return bool + */ + public static function namespaceHasClasses($namespace) + { + self::initialize(); + + return count(self::findersWithNamespace($namespace)) > 0; + } + public static function setAppRoot($appRoot) { self::initialize(); @@ -160,4 +169,18 @@ class ClassFinder return $supportedFinders; } -} \ No newline at end of file + + /** + * @param string $namespace + * + * @return FinderInterface[] + */ + private static function findersWithNamespace($namespace) + { + $findersWithNamespace = array_filter(self::getSupportedFinders(), function (FinderInterface $finder) use ($namespace) { + return $finder->isNamespaceKnown($namespace); + }); + + return $findersWithNamespace; + } +} diff --git a/src/FinderInterface.php b/src/FinderInterface.php index abd6d9e..950d4ab 100644 --- a/src/FinderInterface.php +++ b/src/FinderInterface.php @@ -10,8 +10,8 @@ interface FinderInterface * For instance: * If given a classmap for "TestApp1\Foo\Bar\Baz", the namespace "TestApp1\Foo" is known, even if nothing loads * from that namespace directly. It is known because classes that include that namespace are known. - * @param $namespace + * @param string $namespace * @return bool */ public function isNamespaceKnown($namespace); -} \ No newline at end of file +} diff --git a/test/app1/src/ClassFinderTest.php b/test/app1/src/ClassFinderTest.php index 8539133..9f67f10 100644 --- a/test/app1/src/ClassFinderTest.php +++ b/test/app1/src/ClassFinderTest.php @@ -15,13 +15,10 @@ class ClassFinderTest extends \PHPUnit_Framework_TestCase ClassFinder::setAppRoot(null); } - /** - * @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException - * @expectedExceptionMessageRegExp /Unknown namespace 'DoesNotExist'\./ - */ - public function testThrowsOnUnknownNameSpace() + public function testNoClassesInNamespace() { - ClassFinder::getClassesInNamespace('DoesNotExist'); + $this->assertCount(0, ClassFinder::getClassesInNamespace('DoesNotExist')); + $this->assertFalse(ClassFinder::namespaceHasClasses('DoesNotExist')); } /** @@ -47,4 +44,4 @@ class ClassFinderTest extends \PHPUnit_Framework_TestCase ClassFinder::getClassesInNamespace('TestApp1'); } -} \ No newline at end of file +} diff --git a/test/app1/src/FilesTest.php b/test/app1/src/FilesTest.php index e723fa2..77b197e 100644 --- a/test/app1/src/FilesTest.php +++ b/test/app1/src/FilesTest.php @@ -67,14 +67,11 @@ class FilesTest extends \PHPUnit_Framework_TestCase ); } - /** - * @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException - * @expectedExceptionMessageRegExp /Unknown namespace 'TestApp1\\FilesClasses'\./ - */ public function testFilesSupportRequiresEnabling() { ClassFinder::disableExperimentalFilesSupport(); // Disabling FilesSupport should cause no files to be found. - $classes = ClassFinder::getClassesInNamespace('TestApp1\FilesClasses'); + + $this->assertFalse(ClassFinder::namespaceHasClasses('TestApp1\FilesClasses')); } -} \ No newline at end of file +} diff --git a/test/app1/src/PSR4Test.php b/test/app1/src/PSR4Test.php index 09eb7e4..44e1eb3 100644 --- a/test/app1/src/PSR4Test.php +++ b/test/app1/src/PSR4Test.php @@ -185,13 +185,10 @@ class PSR4Test extends \PHPUnit_Framework_TestCase ); } - /** - * @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException - * @expectedExceptionMessageRegExp /Unknown namespace 'TestApp1\\DoesNotExist'\./ - */ - public function testThrowsOnUnknownSubNameSpace() + public function testForClassesInNamespace() { - ClassFinder::getClassesInNamespace('TestApp1\DoesNotExist'); + $this->assertFalse(ClassFinder::namespaceHasClasses('DoesNotExist')); + $this->assertTrue(ClassFinder::namespaceHasClasses('HaydenPierce\ClassFinder')); } public function testCanFindSelf() @@ -205,4 +202,4 @@ class PSR4Test extends \PHPUnit_Framework_TestCase $this->assertGreaterThan(0, count($classes), 'ClassFinder should be able to find its own internal classes'); } -} \ No newline at end of file +}