From d492de52ef9bfed81c5fc7cc0facd53b527acd05 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Thu, 21 Jul 2022 12:21:37 +0200 Subject: [PATCH] Fix up generics generation. (#273) * Fix up generics generation. --- src/Annotator/ControllerAnnotator.php | 19 +++++++++---------- src/Annotator/ModelAnnotator.php | 21 ++++++++++++++------- src/Utility/ArrayString.php | 12 +++++++++--- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/Annotator/ControllerAnnotator.php b/src/Annotator/ControllerAnnotator.php index feb73218..a284d023 100644 --- a/src/Annotator/ControllerAnnotator.php +++ b/src/Annotator/ControllerAnnotator.php @@ -12,6 +12,7 @@ use IdeHelper\Annotation\PropertyAnnotation; use IdeHelper\Annotator\Traits\ComponentTrait; use IdeHelper\Utility\App; +use IdeHelper\Utility\ArrayString; use RuntimeException; use Throwable; @@ -186,16 +187,14 @@ protected function getUsedComponents(string $className, string $path): array { * @return array<\IdeHelper\Annotation\AbstractAnnotation> */ protected function getPaginationAnnotations(string $content, ?string $primaryModelName): array { - $entityTypehints = $this->extractPaginateEntityTypehints($content, $primaryModelName); - if (!$entityTypehints) { + $entities = $this->extractPaginateEntities($content, $primaryModelName); + if (!$entities) { return []; } - $entityTypehints[] = '\\' . ResultSetInterface::class; + $resultSetInterfaceCollection = ArrayString::generate(implode('|', $entities), '\\' . ResultSetInterface::class); - $type = implode('|', $entityTypehints); - - $annotations = [AnnotationFactory::createOrFail(MethodAnnotation::TAG, $type, 'paginate($object = null, array $settings = [])')]; + $annotations = [AnnotationFactory::createOrFail(MethodAnnotation::TAG, $resultSetInterfaceCollection, 'paginate($object = null, array $settings = [])')]; return $annotations; } @@ -206,7 +205,7 @@ protected function getPaginationAnnotations(string $content, ?string $primaryMod * * @return array */ - protected function extractPaginateEntityTypehints(string $content, ?string $primaryModelName): array { + protected function extractPaginateEntities(string $content, ?string $primaryModelName): array { $models = []; preg_match_all('/\$this->paginate\(\)/i', $content, $matches); @@ -227,11 +226,11 @@ protected function extractPaginateEntityTypehints(string $content, ?string $prim foreach ($models as $model) { $entityClassName = $this->getEntity($model, $primaryModelName); - $typehint = '\\' . ltrim($entityClassName, '\\') . '[]'; - if (in_array($typehint, $result, true)) { + $fullClassName = '\\' . ltrim($entityClassName, '\\'); + if (in_array($fullClassName, $result, true)) { continue; } - $result[] = $typehint; + $result[] = $fullClassName; } return $result; diff --git a/src/Annotator/ModelAnnotator.php b/src/Annotator/ModelAnnotator.php index b9fcf653..1c602a77 100644 --- a/src/Annotator/ModelAnnotator.php +++ b/src/Annotator/ModelAnnotator.php @@ -4,6 +4,8 @@ use Cake\Core\Configure; use Cake\Database\Schema\TableSchemaInterface; +use Cake\Datasource\EntityInterface; +use Cake\Datasource\ResultSetInterface; use Cake\ORM\Association\BelongsToMany; use Cake\ORM\Association\HasMany; use Cake\ORM\AssociationCollection; @@ -156,16 +158,21 @@ protected function buildAnnotations(array $associations, string $entity, array $ $annotations[] = "@method $fullClassName get(\$primaryKey, \$options = [])"; $annotations[] = "@method $fullClassName findOrCreate(\$search, ?callable \$callback = null, \$options = [])"; - $annotations[] = "@method $fullClassName patchEntity(\\Cake\\Datasource\\EntityInterface \$entity, array \$data, array \$options = [])"; + $entityInterface = '\\' . EntityInterface::class; + + $annotations[] = "@method $fullClassName patchEntity({$entityInterface} \$entity, array \$data, array \$options = [])"; $annotations[] = "@method $fullClassNameCollection patchEntities(iterable \$entities, array \$data, array \$options = [])"; - $annotations[] = "@method $fullClassName|false save(\\Cake\\Datasource\\EntityInterface \$entity, \$options = [])"; - $annotations[] = "@method $fullClassName saveOrFail(\\Cake\\Datasource\\EntityInterface \$entity, \$options = [])"; - $annotations[] = "@method {$fullClassName}[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable \$entities, \$options = [])"; - $annotations[] = "@method {$fullClassName}[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable \$entities, \$options = [])"; + $annotations[] = "@method $fullClassName|false save({$entityInterface} \$entity, \$options = [])"; + $annotations[] = "@method $fullClassName saveOrFail({$entityInterface} \$entity, \$options = [])"; + + $resultSetInterfaceCollection = ArrayString::generate($fullClassName, '\\' . ResultSetInterface::class); + + $annotations[] = "@method {$resultSetInterfaceCollection}|false saveMany(iterable \$entities, \$options = [])"; + $annotations[] = "@method {$resultSetInterfaceCollection} saveManyOrFail(iterable \$entities, \$options = [])"; - $annotations[] = "@method {$fullClassName}[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable \$entities, \$options = [])"; - $annotations[] = "@method {$fullClassName}[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable \$entities, \$options = [])"; + $annotations[] = "@method {$resultSetInterfaceCollection}|false deleteMany(iterable \$entities, \$options = [])"; + $annotations[] = "@method {$resultSetInterfaceCollection} deleteManyOrFail(iterable \$entities, \$options = [])"; } // Make replaceable via parsed object diff --git a/src/Utility/ArrayString.php b/src/Utility/ArrayString.php index 2a779559..2d4a7006 100644 --- a/src/Utility/ArrayString.php +++ b/src/Utility/ArrayString.php @@ -8,15 +8,21 @@ class ArrayString { /** * @param string $value + * @param string|null $type * * @return string */ - public static function generate(string $value): string { + public static function generate(string $value, ?string $type = null): string { if (Configure::read('IdeHelper.arrayAsGenerics')) { - return sprintf('array<%s>', $value); + return sprintf(($type ?: 'array') . '<%s>', $value); } - return $value . '[]'; + $value .= '[]'; + if ($type) { + $value .= '|' . $type; + } + + return $value; } }