From 1663ff4426a8a9e17e820a6893f14f22663c0772 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 12:51:13 +0900 Subject: [PATCH 1/5] perf: replace $locator->getClassname() with findQualifiedNameFromPath() --- system/CLI/Commands.php | 2 +- system/Config/BaseConfig.php | 2 +- system/Config/BaseService.php | 4 ++-- system/Config/Factories.php | 2 +- system/Filters/Filters.php | 2 +- system/Publisher/Publisher.php | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index df28e533a354..adcfb265e88b 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -100,7 +100,7 @@ public function discoverCommands() // Loop over each file checking to see if a command with that // alias exists in the class. foreach ($files as $file) { - $className = $locator->getClassname($file); + $className = $locator->findQualifiedNameFromPath($file); if ($className === '' || ! class_exists($className)) { continue; diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 87166737e746..0d15b911499e 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -211,7 +211,7 @@ protected function registerProperties() $registrarsFiles = $locator->search('Config/Registrar.php'); foreach ($registrarsFiles as $file) { - $className = $locator->getClassname($file); + $className = $locator->findQualifiedNameFromPath($file); static::$registrars[] = new $className(); } diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index b66b8c9f535e..e095e22693dd 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -343,7 +343,7 @@ protected static function discoverServices(string $name, array $arguments) // Get instances of all service classes and cache them locally. foreach ($files as $file) { - $classname = $locator->getClassname($file); + $classname = $locator->findQualifiedNameFromPath($file); if (! in_array($classname, [Services::class], true)) { static::$services[] = new $classname(); @@ -380,7 +380,7 @@ protected static function buildServicesCache(): void // Get instances of all service classes and cache them locally. foreach ($files as $file) { - $classname = $locator->getClassname($file); + $classname = $locator->findQualifiedNameFromPath($file); if ($classname !== Services::class) { self::$serviceNames[] = $classname; diff --git a/system/Config/Factories.php b/system/Config/Factories.php index a32df02725ac..69b8f98ccb55 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -297,7 +297,7 @@ class_exists($alias, false) // Check all files for a valid class foreach ($files as $file) { - $class = $locator->getClassname($file); + $class = $locator->findQualifiedNameFromPath($file); if ($class && self::verifyInstanceOf($options, $class)) { return $class; diff --git a/system/Filters/Filters.php b/system/Filters/Filters.php index 665db2e01792..15d6b1beb327 100644 --- a/system/Filters/Filters.php +++ b/system/Filters/Filters.php @@ -139,7 +139,7 @@ private function discoverFilters(): void $files = $locator->search('Config/Filters.php'); foreach ($files as $file) { - $className = $locator->getClassname($file); + $className = $locator->findQualifiedNameFromPath($file); // Don't include our main Filter config again... if ($className === FiltersConfig::class) { diff --git a/system/Publisher/Publisher.php b/system/Publisher/Publisher.php index e40a14c88905..9eb4e8ab1cb6 100644 --- a/system/Publisher/Publisher.php +++ b/system/Publisher/Publisher.php @@ -114,7 +114,7 @@ final public static function discover(string $directory = 'Publishers'): array // Loop over each file checking to see if it is a Publisher foreach (array_unique($files) as $file) { - $className = $locator->getClassname($file); + $className = $locator->findQualifiedNameFromPath($file); if ($className !== '' && class_exists($className) && is_a($className, self::class, true)) { self::$discovered[$directory][] = new $className(); From 10020ff337dce6e10e653d3b55dd23e338e1abcc Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 13:15:10 +0900 Subject: [PATCH 2/5] fix: revert to getClassname() --- system/Filters/Filters.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Filters/Filters.php b/system/Filters/Filters.php index 15d6b1beb327..95a38f313303 100644 --- a/system/Filters/Filters.php +++ b/system/Filters/Filters.php @@ -139,7 +139,8 @@ private function discoverFilters(): void $files = $locator->search('Config/Filters.php'); foreach ($files as $file) { - $className = $locator->findQualifiedNameFromPath($file); + // The $file may not be a class file. + $className = $locator->getClassname($file); // Don't include our main Filter config again... if ($className === FiltersConfig::class) { From 5d63ec4cb04d373381ca1d9a3dc0c1b24ca8391a Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 13:22:38 +0900 Subject: [PATCH 3/5] chore: remove ignoreErrors --- phpstan-baseline.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 64f07d9caeab..4c544984170d 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -951,11 +951,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Config/DotEnv.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Only booleans are allowed in &&, string given on the left side\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Config/Factories.php', -]; $ignoreErrors[] = [ 'message' => '#^Only booleans are allowed in a negated boolean, array given\\.$#', 'count' => 1, From d2321b258a29f2a25cb3196ca20740662fffa2c7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 13:23:08 +0900 Subject: [PATCH 4/5] fix: add false checks --- system/Config/BaseConfig.php | 7 ++++++- system/Config/BaseService.php | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index 0d15b911499e..178d7ce5ca92 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -211,7 +211,12 @@ protected function registerProperties() $registrarsFiles = $locator->search('Config/Registrar.php'); foreach ($registrarsFiles as $file) { - $className = $locator->findQualifiedNameFromPath($file); + $className = $locator->findQualifiedNameFromPath($file); + + if ($className === false) { + continue; + } + static::$registrars[] = new $className(); } diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index e095e22693dd..a15c67806479 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -345,6 +345,10 @@ protected static function discoverServices(string $name, array $arguments) foreach ($files as $file) { $classname = $locator->findQualifiedNameFromPath($file); + if ($classname === false) { + continue; + } + if (! in_array($classname, [Services::class], true)) { static::$services[] = new $classname(); } @@ -382,6 +386,10 @@ protected static function buildServicesCache(): void foreach ($files as $file) { $classname = $locator->findQualifiedNameFromPath($file); + if ($classname === false) { + continue; + } + if ($classname !== Services::class) { self::$serviceNames[] = $classname; static::$services[] = new $classname(); From d3b5c173d393727ff9b14fe709a8410940c45dff Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 6 Oct 2023 13:27:05 +0900 Subject: [PATCH 5/5] fix: update code to check for class existence --- system/CLI/Commands.php | 2 +- system/Config/Factories.php | 2 +- system/Publisher/Publisher.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index adcfb265e88b..caa404bcfe74 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -102,7 +102,7 @@ public function discoverCommands() foreach ($files as $file) { $className = $locator->findQualifiedNameFromPath($file); - if ($className === '' || ! class_exists($className)) { + if ($className === false || ! class_exists($className)) { continue; } diff --git a/system/Config/Factories.php b/system/Config/Factories.php index 69b8f98ccb55..2857fdf06520 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -299,7 +299,7 @@ class_exists($alias, false) foreach ($files as $file) { $class = $locator->findQualifiedNameFromPath($file); - if ($class && self::verifyInstanceOf($options, $class)) { + if ($class !== false && self::verifyInstanceOf($options, $class)) { return $class; } } diff --git a/system/Publisher/Publisher.php b/system/Publisher/Publisher.php index 9eb4e8ab1cb6..064dd354e8ba 100644 --- a/system/Publisher/Publisher.php +++ b/system/Publisher/Publisher.php @@ -116,7 +116,7 @@ final public static function discover(string $directory = 'Publishers'): array foreach (array_unique($files) as $file) { $className = $locator->findQualifiedNameFromPath($file); - if ($className !== '' && class_exists($className) && is_a($className, self::class, true)) { + if ($className !== false && class_exists($className) && is_a($className, self::class, true)) { self::$discovered[$directory][] = new $className(); } }