Skip to content

Commit

Permalink
Fix up generics generation. (#273)
Browse files Browse the repository at this point in the history
* Fix up generics generation.
  • Loading branch information
dereuromark authored Jul 21, 2022
1 parent 8bb230d commit d492de5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
19 changes: 9 additions & 10 deletions src/Annotator/ControllerAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -206,7 +205,7 @@ protected function getPaginationAnnotations(string $content, ?string $primaryMod
*
* @return array<string>
*/
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);
Expand All @@ -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;
Expand Down
21 changes: 14 additions & 7 deletions src/Annotator/ModelAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/Utility/ArrayString.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit d492de5

Please sign in to comment.