From 1d3dbba92a5d7e456d58e6c3138437e1366b1aac Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 09:26:58 +1000 Subject: [PATCH 1/8] Modify approach to dynamic invoker The old approach invoked protected or public static methods. I believe the new approach will allow for the invitation of private methods as well --- tests/ClassFinderConcrete.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/ClassFinderConcrete.php b/tests/ClassFinderConcrete.php index 5d1c7b8..2baf6f3 100644 --- a/tests/ClassFinderConcrete.php +++ b/tests/ClassFinderConcrete.php @@ -29,13 +29,23 @@ public function __construct() self::$vendorDir = ''; } - public function setOptimisedClassMap($value){ + protected static function getMethod($name) + { + $class = new ReflectionClass(self::class); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method; + } + + public function setOptimisedClassMap($value) + { self::$optimisedClassMap = $value; } public function __call($name, $arguments) { - return call_user_func_array([self::class, $name], $arguments); + $method = self::getMethod($name); + return $method->invokeArgs(null, $arguments); } /** From 60553f48c128f85aca12dc8ad0226874a5faf394 Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 09:31:43 +1000 Subject: [PATCH 2/8] Update use statments --- tests/ClassFinderConcrete.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ClassFinderConcrete.php b/tests/ClassFinderConcrete.php index 2baf6f3..4fe5a58 100644 --- a/tests/ClassFinderConcrete.php +++ b/tests/ClassFinderConcrete.php @@ -5,6 +5,7 @@ use Composer\Autoload\ClassLoader; use Cruxinator\ClassFinder\ClassFinder; +use ReflectionClass; /** * Class ClassFinderConcrete. From 9663b7075dad108a955bd1aeab11f00a96a17914 Mon Sep 17 00:00:00 2001 From: c-harris Date: Fri, 21 Feb 2020 23:40:54 +0000 Subject: [PATCH 3/8] Apply fixes from StyleCI --- tests/Unit/ClassFinderTest.php | 56 +++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/tests/Unit/ClassFinderTest.php b/tests/Unit/ClassFinderTest.php index 6ecf0f4..da99f76 100644 --- a/tests/Unit/ClassFinderTest.php +++ b/tests/Unit/ClassFinderTest.php @@ -3,7 +3,6 @@ namespace Tests\Cruxinator\ClassFinder\Unit; - use Cruxinator\ClassFinder\ClassFinder; use Tests\Cruxinator\ClassFinder\ClassFinderConcrete; use Tests\Cruxinator\ClassFinder\TestCase; @@ -20,70 +19,77 @@ public function setUp():void $this->classFinder = new ClassFinderConcrete(); } - public function testSelf(){ - $classes = $this->classFinder->getClasses("Cruxinator\\ClassFinder\\"); - $this->assertEquals(1,count($classes)); + public function testSelf() + { + $classes = $this->classFinder->getClasses('Cruxinator\\ClassFinder\\'); + $this->assertEquals(1, count($classes)); $this->assertEquals(ClassFinder::class, $classes[0]); } - public function testFindPsr(){ - $classes = $this->classFinder->getClasses("Psr\\Log\\"); + public function testFindPsr() + { + $classes = $this->classFinder->getClasses('Psr\\Log\\'); $this->assertTrue(count($classes) > 0); - foreach($classes as $class){ + foreach ($classes as $class) { $this->assertTrue(class_exists($class)); - $this->assertStringStartsWith("Psr\\Log\\",$class); + $this->assertStringStartsWith('Psr\\Log\\', $class); } - $twoClasses = $this->classFinder->getClasses("Psr\\Log\\"); + $twoClasses = $this->classFinder->getClasses('Psr\\Log\\'); $this->assertEquals(count($classes), count($twoClasses)); } - public function testTwoCallsSameFinder(){ + public function testTwoCallsSameFinder() + { $this->testFindPsr(); $this->testSelf(); } - public function testFindPsrNotAbstract(){ - $classes = $this->classFinder->getClasses("Psr\\Log\\", function($class){ + public function testFindPsrNotAbstract() + { + $classes = $this->classFinder->getClasses('Psr\\Log\\', function ($class) { $reflectionClass = new \ReflectionClass($class); return !$reflectionClass->isAbstract(); }); $this->assertTrue(count($classes) > 0); - foreach($classes as $class){ + foreach ($classes as $class) { $this->assertTrue(class_exists($class)); - $this->assertStringStartsWith("Psr\\Log\\",$class); + $this->assertStringStartsWith('Psr\\Log\\', $class); $reflectionClass = new \ReflectionClass($class); $this->assertFalse($reflectionClass->isAbstract()); } } - public function testFindPsrOnlyAbstract(){ - $classes = $this->classFinder->getClasses("Psr\\Log\\", function($class){ + public function testFindPsrOnlyAbstract() + { + $classes = $this->classFinder->getClasses('Psr\\Log\\', function ($class) { $reflectionClass = new \ReflectionClass($class); return $reflectionClass->isAbstract(); }); $this->assertTrue(count($classes) > 0); - foreach($classes as $class){ + foreach ($classes as $class) { $this->assertTrue(class_exists($class)); - $this->assertStringStartsWith("Psr\\Log\\",$class); + $this->assertStringStartsWith('Psr\\Log\\', $class); $reflectionClass = new \ReflectionClass($class); $this->assertTrue($reflectionClass->isAbstract()); } } - public function testFindPsrNotInVender(){ - $classes = $this->classFinder->getClasses("Psr\\Log\\",null,false); + public function testFindPsrNotInVender() + { + $classes = $this->classFinder->getClasses('Psr\\Log\\', null, false); $this->assertFalse(count($classes) > 0); } /** * @runInSeparateProcess */ - public function testErrorCheck(){ + public function testErrorCheck() + { $this->classFinder->setOptimisedClassMap(false); $autoloader = $this->classFinder->getComposerAutoloader(); $classMap = $autoloader->getClassMap(); - if(array_key_exists(__CLASS__,$classMap)){ - $this->markTestSkipped("Error only works with non optimized autoloader"); + if (array_key_exists(__CLASS__, $classMap)) { + $this->markTestSkipped('Error only works with non optimized autoloader'); } $autoloader->unregister(); @@ -92,9 +98,9 @@ public function testErrorCheck(){ $autoloader->register(); $this->fail(); return; - }catch(\Exception $e){ + } catch (\Exception $e) { $autoloader->register(); $this->assertInstanceOf(\Exception::class, $e); } } -} \ No newline at end of file +} From 536b7041b412d42e5ce16641a3d1a420e894cef8 Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 09:49:32 +1000 Subject: [PATCH 4/8] Change internal methods to private --- src/ClassFinder.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ClassFinder.php b/src/ClassFinder.php index c2ca1be..4c56cb3 100644 --- a/src/ClassFinder.php +++ b/src/ClassFinder.php @@ -36,7 +36,7 @@ abstract class ClassFinder * @throws Exception * @return array|string[] an array with the name of the defined classes */ - protected static function getProjectClasses(string $namespace): array + private static function getProjectClasses(string $namespace): array { if (in_array($namespace, self::$loadedNamespaces)) { return get_declared_classes(); @@ -61,7 +61,7 @@ class_exists($class, true); * @throws Exception * @return array|string[] the class map, keyed by Classname values of files */ - protected static function getClassMap(string $namespace): array + private static function getClassMap(string $namespace): array { self::checkState(); if (self::$optimisedClassMap !== false) { @@ -84,7 +84,7 @@ protected static function getClassMap(string $namespace): array * @param string $haystack the input string * @return bool true if haystack starts with needle, false otherwise */ - protected static function strStartsWith(string $needle, string $haystack):bool + private static function strStartsWith(string $needle, string $haystack):bool { return substr($haystack, 0, strlen($needle)) === $needle; } @@ -94,7 +94,7 @@ protected static function strStartsWith(string $needle, string $haystack):bool * * @throws Exception thrown when a combination of components is not available */ - protected static function checkState() : void + private static function checkState() : void { self::initClassMap(); if (false === self::$optimisedClassMap && !class_exists(ClassMapGenerator::class)) { @@ -106,7 +106,7 @@ protected static function checkState() : void /** * Initializes the optimised class map, if possible. */ - protected static function initClassMap() :void + private static function initClassMap() :void { if (null !== self::$optimisedClassMap) { return; @@ -121,7 +121,7 @@ protected static function initClassMap() :void * * @return ClassLoader|null */ - protected static function getComposerAutoloader(): ?ClassLoader + private static function getComposerAutoloader(): ?ClassLoader { $funcs = spl_autoload_functions(); $classLoader = null; @@ -168,7 +168,7 @@ public static function getClasses(string $namespace = '', callable $conditional * @param string $namespace the namespace (without preceding \ * @return array a list of directories containing classes for that namespace */ - protected static function getProjectSearchDirs(string $namespace): array + private static function getProjectSearchDirs(string $namespace): array { $autoloader = self::getComposerAutoloader(); $raw = $autoloader->getPrefixesPsr4(); @@ -182,7 +182,7 @@ protected static function getProjectSearchDirs(string $namespace): array * @throws ReflectionException * @return bool true if in vendor otherwise false */ - protected static function isClassInVendor(string $className) : bool + private static function isClassInVendor(string $className) : bool { $reflection = new ReflectionClass($className); $filename = $reflection->getFileName(); From 9e3ac1b74c3223b4b90faded45441e2150afff8c Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 09:55:12 +1000 Subject: [PATCH 5/8] Removed Unused mock framework --- tests/ClassFinderConcrete.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tests/ClassFinderConcrete.php b/tests/ClassFinderConcrete.php index 4fe5a58..14f7787 100644 --- a/tests/ClassFinderConcrete.php +++ b/tests/ClassFinderConcrete.php @@ -21,8 +21,6 @@ */ class ClassFinderConcrete extends ClassFinder { - protected static $mockClassLoader = null; - public function __construct() { self::$loadedNamespaces = []; @@ -48,22 +46,4 @@ public function __call($name, $arguments) $method = self::getMethod($name); return $method->invokeArgs(null, $arguments); } - - /** - * Gets a dynamically assigned autoloader. - * - * @return ClassLoader|null - */ - protected static function getComposerAutoloader(): ?ClassLoader - { - if (self::$mockClassLoader !== null) { - return self::$mockClassLoader; - } - return parent::getComposerAutoloader(); - } - - protected static function setMockClassLoader($mockObject) - { - self::$mockClassLoader = $mockObject; - } } From c77285c58919f091f6e387518801d457a6318a8c Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 10:14:30 +1000 Subject: [PATCH 6/8] Added missing type hint --- src/ClassFinder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ClassFinder.php b/src/ClassFinder.php index 4c56cb3..0dd48da 100644 --- a/src/ClassFinder.php +++ b/src/ClassFinder.php @@ -25,7 +25,7 @@ abstract class ClassFinder */ protected static $vendorDir = ''; /** - * @var null|array|string[] + * @var null|array|string[]|bool */ protected static $optimisedClassMap = null; From 2f88406308720afcf2bd4ea41dcf86018b75380e Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 10:25:36 +1000 Subject: [PATCH 7/8] Extend test to check message --- tests/Unit/ClassFinderTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Unit/ClassFinderTest.php b/tests/Unit/ClassFinderTest.php index da99f76..f74c47e 100644 --- a/tests/Unit/ClassFinderTest.php +++ b/tests/Unit/ClassFinderTest.php @@ -101,6 +101,9 @@ public function testErrorCheck() } catch (\Exception $e) { $autoloader->register(); $this->assertInstanceOf(\Exception::class, $e); + $this->assertContains('Cruxinator/ClassFinder', $e->getMessage()); + $this->assertContains('composer/composer', $e->getMessage()); + $this->assertContains('composer dump-autoload -o', $e->getMessage()); } } } From 3c374681d5ac6e0fab9315fc49697c197a83bc0b Mon Sep 17 00:00:00 2001 From: c-harris Date: Sat, 22 Feb 2020 10:35:29 +1000 Subject: [PATCH 8/8] Removed php-unit 9 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7241d9b..46d0bcf 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "composer/composer": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0", + "phpunit/phpunit": "^6.0|^7.0|^8.0", "composer/composer": "^1.0", "php-coveralls/php-coveralls": "^2.2" },