1
0
mirror of https://github.com/danog/class-finder.git synced 2024-11-30 04:29:03 +01:00

Add ClassFinder::namespaceHasClasses instead of throwing on empty namespaces

This commit is contained in:
Benedikt Franke 2019-09-15 14:28:01 +00:00 committed by Hayden Pierce
parent 4f2d91aa87
commit 841294cd53
10 changed files with 67 additions and 40 deletions

View File

@ -97,7 +97,6 @@ Documentation
* [Files could not locate PHP](docs/exceptions/filesCouldNotLocatePHP.md) * [Files could not locate PHP](docs/exceptions/filesCouldNotLocatePHP.md)
* [Files exec not available](docs/exceptions/filesExecNotAvailable.md) * [Files exec not available](docs/exceptions/filesExecNotAvailable.md)
* [Missing composer.json](docs/exceptions/missingComposerConfig.md) * [Missing composer.json](docs/exceptions/missingComposerConfig.md)
* [Unknown namespace](docs/exceptions/unknownNamespace.md)
**Internals** **Internals**

View File

@ -1,3 +1,6 @@
#!/usr/bin/env bash
set -Eeuxo pipefail
composer install --working-dir=$1/test/app1 --quiet || exit 1 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/test/app2 --quiet || exit 1
composer install --working-dir=$1 --quiet || exit 1 composer install --working-dir=$1 --quiet || exit 1

View File

@ -1,3 +1,6 @@
#!/usr/bin/env bash
set -Eeuxo pipefail
composer install --working-dir=$1/test/app1 --quiet || exit 1 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/test/app2 --quiet || exit 1
composer install --working-dir=$1 --quiet || exit 1 composer install --working-dir=$1 --quiet || exit 1

View File

@ -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 Version 0.3.3
------------- -------------

View File

@ -1,6 +1,8 @@
Unknown Namespace Unknown Namespace
----------------- -----------------
__This exception only occurs in versions 0.3.x and lower.__
Example PHP: Example PHP:
``` ```
<?php <?php

View File

@ -58,25 +58,20 @@ class ClassFinder
} }
/** /**
* @param $namespace * Identify classes in a given namespace.
* @param $options *
* @return array * @param string $namespace
* @param int $options
*
* @return string[]
*
* @throws \Exception * @throws \Exception
*/ */
public static function getClassesInNamespace($namespace, $options = self::STANDARD_MODE) public static function getClassesInNamespace($namespace, $options = self::STANDARD_MODE)
{ {
self::initialize(); self::initialize();
$findersWithNamespace = array_filter(self::getSupportedFinders(), function(FinderInterface $finder) use ($namespace){ $findersWithNamespace = self::findersWithNamespace($namespace);
return $finder->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'
));
}
$classes = array_reduce($findersWithNamespace, function($carry, FinderInterface $finder) use ($namespace, $options){ $classes = array_reduce($findersWithNamespace, function($carry, FinderInterface $finder) use ($namespace, $options){
return array_merge($carry, $finder->findClasses($namespace, $options)); return array_merge($carry, $finder->findClasses($namespace, $options));
@ -85,6 +80,20 @@ class ClassFinder
return array_unique($classes); 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) public static function setAppRoot($appRoot)
{ {
self::initialize(); self::initialize();
@ -160,4 +169,18 @@ class ClassFinder
return $supportedFinders; return $supportedFinders;
} }
/**
* @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;
}
} }

View File

@ -10,7 +10,7 @@ interface FinderInterface
* For instance: * For instance:
* If given a classmap for "TestApp1\Foo\Bar\Baz", the namespace "TestApp1\Foo" is known, even if nothing loads * 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. * from that namespace directly. It is known because classes that include that namespace are known.
* @param $namespace * @param string $namespace
* @return bool * @return bool
*/ */
public function isNamespaceKnown($namespace); public function isNamespaceKnown($namespace);

View File

@ -15,13 +15,10 @@ class ClassFinderTest extends \PHPUnit_Framework_TestCase
ClassFinder::setAppRoot(null); ClassFinder::setAppRoot(null);
} }
/** public function testNoClassesInNamespace()
* @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException
* @expectedExceptionMessageRegExp /Unknown namespace 'DoesNotExist'\./
*/
public function testThrowsOnUnknownNameSpace()
{ {
ClassFinder::getClassesInNamespace('DoesNotExist'); $this->assertCount(0, ClassFinder::getClassesInNamespace('DoesNotExist'));
$this->assertFalse(ClassFinder::namespaceHasClasses('DoesNotExist'));
} }
/** /**

View File

@ -67,14 +67,11 @@ class FilesTest extends \PHPUnit_Framework_TestCase
); );
} }
/**
* @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException
* @expectedExceptionMessageRegExp /Unknown namespace 'TestApp1\\FilesClasses'\./
*/
public function testFilesSupportRequiresEnabling() public function testFilesSupportRequiresEnabling()
{ {
ClassFinder::disableExperimentalFilesSupport(); // Disabling FilesSupport should cause no files to be found. ClassFinder::disableExperimentalFilesSupport(); // Disabling FilesSupport should cause no files to be found.
$classes = ClassFinder::getClassesInNamespace('TestApp1\FilesClasses');
$this->assertFalse(ClassFinder::namespaceHasClasses('TestApp1\FilesClasses'));
} }
} }

View File

@ -185,13 +185,10 @@ class PSR4Test extends \PHPUnit_Framework_TestCase
); );
} }
/** public function testForClassesInNamespace()
* @expectedException HaydenPierce\ClassFinder\Exception\ClassFinderException
* @expectedExceptionMessageRegExp /Unknown namespace 'TestApp1\\DoesNotExist'\./
*/
public function testThrowsOnUnknownSubNameSpace()
{ {
ClassFinder::getClassesInNamespace('TestApp1\DoesNotExist'); $this->assertFalse(ClassFinder::namespaceHasClasses('DoesNotExist'));
$this->assertTrue(ClassFinder::namespaceHasClasses('HaydenPierce\ClassFinder'));
} }
public function testCanFindSelf() public function testCanFindSelf()