From af0db30d7abf7226418ea4614ce0b29a25de42cf Mon Sep 17 00:00:00 2001 From: mscherer Date: Mon, 25 Nov 2024 13:25:41 +0100 Subject: [PATCH] Fix plugin wildcard for TableCallbacks. --- .../TableCallbackAnnotatorTask.php | 2 +- src/Annotator/CommandAnnotator.php | 2 +- src/Annotator/ComponentAnnotator.php | 2 +- src/Annotator/ControllerAnnotator.php | 8 ++++---- src/Annotator/HelperAnnotator.php | 2 +- src/Annotator/ModelAnnotator.php | 2 +- src/Annotator/Traits/DocBlockTrait.php | 8 ++++---- src/Command/AnnotateCommand.php | 17 ++++------------- src/Filesystem/Folder.php | 4 ++-- src/Generator/Task/FixtureTask.php | 2 +- 10 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Annotator/CallbackAnnotatorTask/TableCallbackAnnotatorTask.php b/src/Annotator/CallbackAnnotatorTask/TableCallbackAnnotatorTask.php index 2e0b3a40..4e752d16 100644 --- a/src/Annotator/CallbackAnnotatorTask/TableCallbackAnnotatorTask.php +++ b/src/Annotator/CallbackAnnotatorTask/TableCallbackAnnotatorTask.php @@ -37,7 +37,7 @@ class TableCallbackAnnotatorTask extends AbstractCallbackAnnotatorTask implement */ public function shouldRun(string $path): bool { $className = pathinfo($path, PATHINFO_FILENAME); - if ($className === 'Table' || substr($className, -5) !== 'Table') { + if ($className === 'Table' || !str_ends_with($className, 'Table')) { return false; } diff --git a/src/Annotator/CommandAnnotator.php b/src/Annotator/CommandAnnotator.php index 5b29f74b..7a7bc28a 100644 --- a/src/Annotator/CommandAnnotator.php +++ b/src/Annotator/CommandAnnotator.php @@ -15,7 +15,7 @@ class CommandAnnotator extends AbstractAnnotator { */ public function annotate(string $path): bool { $className = pathinfo($path, PATHINFO_FILENAME); - if ($className === 'Command' || substr($className, -7) !== 'Command') { + if ($className === 'Command' || !str_ends_with($className, 'Command')) { return false; } diff --git a/src/Annotator/ComponentAnnotator.php b/src/Annotator/ComponentAnnotator.php index 20178b69..8822e414 100644 --- a/src/Annotator/ComponentAnnotator.php +++ b/src/Annotator/ComponentAnnotator.php @@ -24,7 +24,7 @@ class ComponentAnnotator extends AbstractAnnotator { */ public function annotate(string $path): bool { $name = pathinfo($path, PATHINFO_FILENAME); - if (substr($name, -9) !== 'Component') { + if (!str_ends_with($name, 'Component')) { return false; } diff --git a/src/Annotator/ControllerAnnotator.php b/src/Annotator/ControllerAnnotator.php index 0a6c530d..aa406760 100644 --- a/src/Annotator/ControllerAnnotator.php +++ b/src/Annotator/ControllerAnnotator.php @@ -29,7 +29,7 @@ class ControllerAnnotator extends AbstractAnnotator { */ public function annotate(string $path): bool { $className = pathinfo($path, PATHINFO_FILENAME); - if (substr($className, -10) !== 'Controller') { + if (!str_ends_with($className, 'Controller')) { return false; } @@ -119,7 +119,7 @@ protected function getComponentAnnotations(string $className, string $path): arr $annotations = []; foreach ($map as $component => $className) { - if (substr($className, 0, 5) === 'Cake\\') { + if (str_starts_with($className, 'Cake\\')) { continue; } @@ -267,10 +267,10 @@ protected function getEntity(string $modelName, ?string $primaryModelName): stri * @return string|null */ protected function findModelClass(string $className, string $path): ?string { - $plugin = $this->getConfig(static::CONFIG_PLUGIN) ? $this->getConfig(static::CONFIG_PLUGIN) . '.' : ''; + $plugin = $this->getConfig(static::CONFIG_PLUGIN); $prefix = $this->getPrefix($className, $path); - $fullClassName = App::className($plugin . $className, 'Controller' . $prefix); + $fullClassName = App::className(($plugin ? $plugin . '.' : '') . $className, 'Controller' . $prefix); if (!$fullClassName) { return null; } diff --git a/src/Annotator/HelperAnnotator.php b/src/Annotator/HelperAnnotator.php index f6c4e740..a836fac7 100644 --- a/src/Annotator/HelperAnnotator.php +++ b/src/Annotator/HelperAnnotator.php @@ -20,7 +20,7 @@ class HelperAnnotator extends AbstractAnnotator { */ public function annotate(string $path): bool { $name = pathinfo($path, PATHINFO_FILENAME); - if (substr($name, -6) !== 'Helper') { + if (!str_ends_with($name, 'Helper')) { return false; } diff --git a/src/Annotator/ModelAnnotator.php b/src/Annotator/ModelAnnotator.php index 3908521d..99d7f67c 100644 --- a/src/Annotator/ModelAnnotator.php +++ b/src/Annotator/ModelAnnotator.php @@ -33,7 +33,7 @@ class ModelAnnotator extends AbstractAnnotator { */ public function annotate(string $path): bool { $className = pathinfo($path, PATHINFO_FILENAME); - if ($className === 'Table' || substr($className, -5) !== 'Table') { + if ($className === 'Table' || !str_ends_with($className, 'Table')) { return false; } diff --git a/src/Annotator/Traits/DocBlockTrait.php b/src/Annotator/Traits/DocBlockTrait.php index 3f68df45..8b53a966 100644 --- a/src/Annotator/Traits/DocBlockTrait.php +++ b/src/Annotator/Traits/DocBlockTrait.php @@ -129,11 +129,11 @@ protected function stringifyValueNode(array $parts, PhpDocTagValueNode $valueNod * @param \PHP_CodeSniffer\Files\File $phpCsFile * @param int $docBlockStartIndex * @param int $docBlockEndIndex - * @param string $needle + * @param string $alias * * @return bool */ - protected function hasInheritDoc(File $phpCsFile, int $docBlockStartIndex, int $docBlockEndIndex, string $needle = '@inheritDoc'): bool { + protected function hasInheritDoc(File $phpCsFile, int $docBlockStartIndex, int $docBlockEndIndex, string $alias = '@inheritDoc'): bool { $tokens = $phpCsFile->getTokens(); for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; ++$i) { @@ -141,12 +141,12 @@ protected function hasInheritDoc(File $phpCsFile, int $docBlockStartIndex, int $ continue; } $content = $tokens[$i]['content']; - $pos = stripos($content, $needle); + $pos = stripos($content, $alias); if ($pos === false) { continue; } - if ($pos && str_starts_with($needle, '@') && substr($content, $pos - 1, $pos) === '{') { + if ($pos && str_starts_with($alias, '@') && substr($content, $pos - 1, $pos) === '{') { return false; } diff --git a/src/Command/AnnotateCommand.php b/src/Command/AnnotateCommand.php index 63127c95..22b89767 100644 --- a/src/Command/AnnotateCommand.php +++ b/src/Command/AnnotateCommand.php @@ -30,11 +30,6 @@ abstract class AnnotateCommand extends Command { ], ]; - /** - * @var array - */ - protected array $_instantiatedAnnotators = []; - /** * @return void */ @@ -160,16 +155,12 @@ protected function getAnnotator(string $class): AbstractAnnotator { $class = $tasks[$class]; } - if (!isset($this->_instantiatedAnnotators[$class])) { - assert($this->args !== null, 'Args not set'); - - $options = $this->args->getOptions(); - $options['plugin'] = $this->plugin; + assert($this->args !== null, 'Args not set'); - $this->_instantiatedAnnotators[$class] = new $class($this->_io(), $options); - } + $options = $this->args->getOptions(); + $options['plugin'] = $this->plugin; - return $this->_instantiatedAnnotators[$class]; + return new $class($this->_io(), $options); } /** diff --git a/src/Filesystem/Folder.php b/src/Filesystem/Folder.php index 67ff3691..8d3812be 100644 --- a/src/Filesystem/Folder.php +++ b/src/Filesystem/Folder.php @@ -291,7 +291,7 @@ protected function _findRecursive(string $pattern, bool $sort = false): array { * @return bool true if windows path, false otherwise */ public static function isWindowsPath(string $path): bool { - return preg_match('/^[A-Z]:\\\\/i', $path) || substr($path, 0, 2) === '\\\\'; + return preg_match('/^[A-Z]:\\\\/i', $path) || str_starts_with($path, '\\\\'); } /** @@ -307,7 +307,7 @@ public static function isAbsolute(string $path): bool { return $path[0] === '/' || preg_match('/^[A-Z]:\\\\/i', $path) || - substr($path, 0, 2) === '\\\\' || + str_starts_with($path, '\\\\') || static::isRegisteredStreamWrapper($path); } diff --git a/src/Generator/Task/FixtureTask.php b/src/Generator/Task/FixtureTask.php index 8cbe4505..321197c3 100644 --- a/src/Generator/Task/FixtureTask.php +++ b/src/Generator/Task/FixtureTask.php @@ -76,7 +76,7 @@ protected function parseFixtures(string $fixtureFolder, string $domain, ?string $fixtures = []; foreach ($content[1] as $file) { - if (substr($file, -11) !== 'Fixture.php') { + if (!str_ends_with($file, 'Fixture.php')) { continue; } $fixture = substr($file, 0, -11);