Skip to content

Commit

Permalink
Merge pull request #8012 from kenjis/perf-replace-locator-getClassname
Browse files Browse the repository at this point in the history
perf: replace $locator->getClassname() with findQualifiedNameFromPath()
  • Loading branch information
kenjis authored Oct 16, 2023
2 parents 1277cae + d3b5c17 commit d5ad48e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ 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)) {
if ($className === false || ! class_exists($className)) {
continue;
}

Expand Down
7 changes: 6 additions & 1 deletion system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ protected function registerProperties()
$registrarsFiles = $locator->search('Config/Registrar.php');

foreach ($registrarsFiles as $file) {
$className = $locator->getClassname($file);
$className = $locator->findQualifiedNameFromPath($file);

if ($className === false) {
continue;
}

static::$registrars[] = new $className();
}

Expand Down
12 changes: 10 additions & 2 deletions system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ 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 ($classname === false) {
continue;
}

if (! in_array($classname, [Services::class], true)) {
static::$services[] = new $classname();
Expand Down Expand Up @@ -380,7 +384,11 @@ 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 === false) {
continue;
}

if ($classname !== Services::class) {
self::$serviceNames[] = $classname;
Expand Down
4 changes: 2 additions & 2 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ 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)) {
if ($class !== false && self::verifyInstanceOf($options, $class)) {
return $class;
}
}
Expand Down
1 change: 1 addition & 0 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private function discoverFilters(): void
$files = $locator->search('Config/Filters.php');

foreach ($files as $file) {
// The $file may not be a class file.
$className = $locator->getClassname($file);

// Don't include our main Filter config again...
Expand Down
4 changes: 2 additions & 2 deletions system/Publisher/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ 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)) {
if ($className !== false && class_exists($className) && is_a($className, self::class, true)) {
self::$discovered[$directory][] = new $className();
}
}
Expand Down

0 comments on commit d5ad48e

Please sign in to comment.