diff --git a/.phive/phars.xml b/.phive/phars.xml index 848f478f..da4f8513 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -1,4 +1,4 @@ - + diff --git a/composer.json b/composer.json index 6ad33444..352018c4 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require-dev": { "cakephp/cakephp-codesniffer": "^5.0.0", "cakephp/debug_kit": "^5.0.0", - "phpunit/phpunit": "^10.5.40 || ^11.5.20 || ^12.2.4" + "phpunit/phpunit": "^10.5.40 || ^11.5.20 || ^12.2.4 || ^13.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index 49655075..1400214e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,5 +7,3 @@ parameters: - src/ bootstrapFiles: - tests/bootstrap.php - ignoreErrors: - - identifier: missingType.iterableValue diff --git a/src/CodeGen/CodeParser.php b/src/CodeGen/CodeParser.php index d3cc0e50..23abd5e0 100644 --- a/src/CodeGen/CodeParser.php +++ b/src/CodeGen/CodeParser.php @@ -49,6 +49,9 @@ class CodeParser extends NodeVisitorAbstract protected string $fileText = ''; + /** + * @var array + */ protected array $parsed = []; /** diff --git a/src/Command/ControllerCommand.php b/src/Command/ControllerCommand.php index 524450c1..cc460bc4 100644 --- a/src/Command/ControllerCommand.php +++ b/src/Command/ControllerCommand.php @@ -167,7 +167,7 @@ public function bake(string $controllerName, Arguments $args, ConsoleIo $io): vo * Generate the controller code * * @param string $controllerName The name of the controller. - * @param array $data The data to turn into code. + * @param array $data The data to turn into code. * @param \Cake\Console\Arguments $args The console args * @param \Cake\Console\ConsoleIo $io The console io * @return void diff --git a/src/Command/EntryCommand.php b/src/Command/EntryCommand.php index 7f8490ad..11f69c54 100644 --- a/src/Command/EntryCommand.php +++ b/src/Command/EntryCommand.php @@ -61,7 +61,7 @@ public function setCommandCollection(CommandCollection $commands): void * * Override the run() method for special handling of the `--help` option. * - * @param array $argv Arguments from the CLI environment. + * @param array $argv Arguments from the CLI environment. * @param \Cake\Console\ConsoleIo $io The console io * @return int|null Exit code or null for success. */ diff --git a/src/Command/FixtureCommand.php b/src/Command/FixtureCommand.php index 1ae33678..3a5860c0 100644 --- a/src/Command/FixtureCommand.php +++ b/src/Command/FixtureCommand.php @@ -285,19 +285,19 @@ protected function _generateSchema(TableSchemaInterface $table): string { $cols = $indexes = $constraints = []; foreach ($table->columns() as $field) { - /** @var array $fieldData */ + /** @var array $fieldData */ $fieldData = $table->getColumn($field); $properties = implode(', ', $this->_values($fieldData)); $cols[] = " '{$field}' => [{$properties}],"; } foreach ($table->indexes() as $index) { - /** @var array $fieldData */ + /** @var array $fieldData */ $fieldData = $table->getIndex($index); $properties = implode(', ', $this->_values($fieldData)); $indexes[] = " '{$index}' => [{$properties}],"; } foreach ($table->constraints() as $index) { - /** @var array $fieldData */ + /** @var array $fieldData */ $fieldData = $table->getConstraint($index); $properties = implode(', ', $this->_values($fieldData)); $constraints[] = " '{$index}' => [{$properties}],"; @@ -324,7 +324,7 @@ protected function _generateSchema(TableSchemaInterface $table): string /** * Formats Schema columns from Model Object * - * @param array $values options keys(type, null, default, key, length, extra) + * @param array $values options keys(type, null, default, key, length, extra) * @return array Formatted values */ protected function _values(array $values): array @@ -351,7 +351,7 @@ protected function _values(array $values): array * * @param \Cake\Database\Schema\TableSchemaInterface $table Table schema array * @param int $recordCount The number of records to generate. - * @return array Array of records to use in the fixture. + * @return array> Array of records to use in the fixture. */ protected function _generateRecords(TableSchemaInterface $table, int $recordCount = 1): array { @@ -359,7 +359,7 @@ protected function _generateRecords(TableSchemaInterface $table, int $recordCoun for ($i = 0; $i < $recordCount; $i++) { $record = []; foreach ($table->columns() as $field) { - /** @var array $fieldInfo */ + /** @var array $fieldInfo */ $fieldInfo = $table->getColumn($field); $insert = ''; switch ($fieldInfo['type']) { @@ -459,7 +459,7 @@ protected function _generateRecords(TableSchemaInterface $table, int $recordCoun /** * Convert a $records array into a string. * - * @param array $records Array of records to be converted to string + * @param array $records Array of records to be converted to string * @return string A string value of the $records array. * @throws \Brick\VarExporter\ExportException */ @@ -485,7 +485,7 @@ protected function _makeRecordString(array $records): string * @param \Cake\Console\Arguments $args CLI arguments * @param string $modelName name of the model to take records from. * @param string|null $useTable Name of table to use. - * @return array Array of records. + * @return array Array of records. */ protected function _getRecordsFromTable(Arguments $args, string $modelName, ?string $useTable = null): array { diff --git a/src/Command/ModelCommand.php b/src/Command/ModelCommand.php index 26e06102..2a18fd73 100644 --- a/src/Command/ModelCommand.php +++ b/src/Command/ModelCommand.php @@ -150,7 +150,7 @@ public function validateNames(TableSchemaInterface $schema, ConsoleIo $io): void * @param string $name The model name to generate. * @param \Cake\Console\Arguments $args CLI Arguments * @param \Cake\Console\ConsoleIo $io CLI io - * @return array + * @return array */ public function getTableContext( Table $tableObject, @@ -221,7 +221,7 @@ public function getTableObject(string $className, string $table): Table * @param \Cake\ORM\Table $table The table to get associations for. * @param \Cake\Console\Arguments $args CLI Arguments * @param \Cake\Console\ConsoleIo $io CLI io - * @return array + * @return array> */ public function getAssociations(Table $table, Arguments $args, ConsoleIo $io): array { @@ -265,7 +265,7 @@ public function getAssociations(Table $table, Arguments $args, ConsoleIo $io): a * generic table object means fields will be detected correctly. * * @param \Cake\ORM\Table $model The table to apply associations to. - * @param array $associations The associations to append. + * @param array> $associations The associations to append. * @return void */ public function applyAssociations(Table $model, array $associations): void @@ -299,7 +299,7 @@ public function applyAssociations(Table $model, array $associations): void * ``` * * @param \Cake\ORM\Table $table The table from which to collect association information. - * @return array A map of association information. + * @return array A map of association information. */ public function getAssociationInfo(Table $table): array { @@ -336,9 +336,9 @@ public function getAssociationInfo(Table $table): array * Find belongsTo relations and add them to the associations list. * * @param \Cake\ORM\Table $model Database\Table instance of table being generated. - * @param array $associations Array of in progress associations + * @param array> $associations Array of in progress associations * @param \Cake\Console\Arguments|null $args CLI arguments - * @return array Associations with belongsTo added in. + * @return array> Associations with belongsTo added in. */ public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array { @@ -495,8 +495,8 @@ public function hasUniqueConstraintFor(TableSchemaInterface $schema, string $key * Find the hasOne relations and add them to associations list * * @param \Cake\ORM\Table $model Model instance being generated - * @param array $associations Array of in progress associations - * @return array Associations with hasOne added in. + * @param array> $associations Array of in progress associations + * @return array> Associations with hasOne added in. */ public function findHasOne(Table $model, array $associations): array { @@ -542,8 +542,8 @@ public function findHasOne(Table $model, array $associations): array * Find the hasMany relations and add them to associations list * * @param \Cake\ORM\Table $model Model instance being generated - * @param array $associations Array of in progress associations - * @return array Associations with hasMany added in. + * @param array> $associations Array of in progress associations + * @return array> Associations with hasMany added in. */ public function findHasMany(Table $model, array $associations): array { @@ -596,8 +596,8 @@ public function findHasMany(Table $model, array $associations): array * Find the BelongsToMany relations and add them to associations list * * @param \Cake\ORM\Table $model Model instance being generated - * @param array $associations Array of in-progress associations - * @return array Associations with belongsToMany added in. + * @param array> $associations Array of in-progress associations + * @return array> Associations with belongsToMany added in. */ public function findBelongsToMany(Table $model, array $associations): array { @@ -691,7 +691,7 @@ public function getPrimaryKey(Table $model, Arguments $args): array * class. * * @param \Cake\ORM\Table $model The model to introspect. - * @return array The property schema + * @return array> The property schema */ public function getEntityPropertySchema(Table $model): array { @@ -699,7 +699,7 @@ public function getEntityPropertySchema(Table $model): array $schema = $model->getSchema(); foreach ($schema->columns() as $column) { - /** @var array $columnSchema */ + /** @var array $columnSchema */ $columnSchema = $schema->getColumn($column); $properties[$column] = [ @@ -799,9 +799,9 @@ public function getHiddenFields(Table $model, Arguments $args): array * Generate default validation rules. * * @param \Cake\ORM\Table $model The model to introspect. - * @param array $associations The associations list. + * @param array> $associations The associations list. * @param \Cake\Console\Arguments $args CLI Arguments - * @return array|false The validation rules. + * @return array>|false The validation rules. */ public function getValidation(Table $model, array $associations, Arguments $args): array|false { @@ -843,9 +843,9 @@ public function getValidation(Table $model, array $associations, Arguments $args * * @param \Cake\Database\Schema\TableSchemaInterface $schema The table schema for the current field. * @param string $fieldName Name of field to be validated. - * @param array $metaData metadata for field + * @param array $metaData metadata for field * @param array $primaryKey The primary key field. Unused because PK validation is skipped - * @return array Array of validation for the field. + * @return array> Array of validation for the field. */ public function fieldValidation( TableSchemaInterface $schema, @@ -930,9 +930,11 @@ public function fieldValidation( } foreach ($schema->constraints() as $constraint) { - /** @var array $constraint */ $constraint = $schema->getConstraint($constraint); - if (!in_array($fieldName, $constraint['columns'] ?? [], true) || count($constraint['columns']) > 1) { + if ( + $constraint === null + || !in_array($fieldName, $constraint['columns'] ?? [], true) || count($constraint['columns']) > 1 + ) { continue; } @@ -958,7 +960,7 @@ public function fieldValidation( * Get the specific allow empty method for field based on metadata. * * @param string $fieldName Field name. - * @param array $metaData Field meta data. + * @param array $metaData Field meta data. * @param string $prefix Method name prefix. * @return string */ @@ -990,9 +992,9 @@ protected function getEmptyMethod(string $fieldName, array $metaData, string $pr * Generate default rules checker. * * @param \Cake\ORM\Table $model The model to introspect. - * @param array $associations The associations for the model. + * @param array> $associations The associations for the model. * @param \Cake\Console\Arguments $args CLI Arguments - * @return array The rules to be applied. + * @return list> The rules to be applied. */ public function getRules(Table $model, array $associations, Arguments $args): array { @@ -1009,9 +1011,8 @@ public function getRules(Table $model, array $associations, Arguments $args): ar $uniqueConstraintsColumns = []; foreach ($schema->constraints() as $name) { - /** @var array $constraint */ $constraint = $schema->getConstraint($name); - if ($constraint['type'] !== TableSchema::CONSTRAINT_UNIQUE) { + if ($constraint === null || $constraint['type'] !== TableSchema::CONSTRAINT_UNIQUE) { continue; } @@ -1079,7 +1080,7 @@ public function getRules(Table $model, array $associations, Arguments $args): ar * Get behaviors * * @param \Cake\ORM\Table $model The model to generate behaviors for. - * @return array Behaviors + * @return array> Behaviors */ public function getBehaviors(Table $model): array { @@ -1115,7 +1116,7 @@ public function getBehaviors(Table $model): array * Get CounterCaches * * @param \Cake\ORM\Table $model The table to get counter cache fields for. - * @return array CounterCache configurations + * @return array> CounterCache configurations */ public function getCounterCache(Table $model): array { @@ -1472,7 +1473,7 @@ protected function possibleEnumFields(TableSchemaInterface $schema): array $fields = []; foreach ($schema->columns() as $column) { - /** @var array $columnSchema */ + /** @var array $columnSchema */ $columnSchema = $schema->getColumn($column); if (str_starts_with((string)$columnSchema['type'], 'enum-')) { $fields[] = $column; @@ -1498,11 +1499,11 @@ protected function getEnumDefinitions(TableSchemaInterface $schema): array $enums = []; foreach ($schema->columns() as $column) { - /** @var array $columnSchema */ $columnSchema = $schema->getColumn($column); if ( - !in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true) - && !str_starts_with((string)$columnSchema['type'], 'enum-') + $columnSchema === null + || (!in_array($columnSchema['type'], ['string', 'integer', 'tinyinteger', 'smallinteger'], true) + && !str_starts_with((string)$columnSchema['type'], 'enum-')) ) { continue; } @@ -1576,8 +1577,8 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console } /** - * @param array> $associations - * @return array> + * @param array> $associations + * @return array> */ protected function ensureAliasUniqueness(array $associations): array { diff --git a/src/Command/TemplateCommand.php b/src/Command/TemplateCommand.php index cf136724..ae96888f 100644 --- a/src/Command/TemplateCommand.php +++ b/src/Command/TemplateCommand.php @@ -266,7 +266,7 @@ protected function _methodsToBake(): array * - 'schema' * * @param \Cake\Console\ConsoleIo $io Instance of the ConsoleIO - * @return array Returns variables to be made available to a view template + * @return array Returns variables to be made available to a view template */ protected function _loadController(ConsoleIo $io): array { @@ -303,7 +303,14 @@ protected function _loadController(ConsoleIo $io): array } $associations = $this->_filteredAssociations($modelObject); $keyFields = []; - if (!empty($associations['BelongsTo'])) { + + if (isset($associations['BelongsToMany'])) { + foreach ($associations['BelongsToMany'] as $assoc) { + $keyFields[$assoc['foreignKey']] = $assoc['variable']; + } + } + + if (isset($associations['BelongsTo'])) { foreach ($associations['BelongsTo'] as $assoc) { $keyFields[$assoc['foreignKey']] = $assoc['variable']; } @@ -379,7 +386,7 @@ public function bake( * @param \Cake\Console\Arguments $args The CLI arguments * @param \Cake\Console\ConsoleIo $io The console io * @param string $action name to generate content to - * @param array|null $vars passed for use in templates + * @param array|null $vars passed for use in templates * @return string Content from template */ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?array $vars = null): string @@ -412,7 +419,7 @@ public function getContent(Arguments $args, ConsoleIo $io, string $action, ?arra $useDomain = (bool)$this->plugin; $renderer->set('useDomain', $useDomain); - return $renderer->generate("Bake.Template/$action"); + return $renderer->generate("Bake.Template/{$action}"); } /** @@ -450,7 +457,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption * To be mocked... * * @param \Cake\ORM\Table $model Table - * @return array associations + * @return array> associations */ protected function _filteredAssociations(Table $model): array { diff --git a/src/Command/TestCommand.php b/src/Command/TestCommand.php index c62c4c19..6dcee136 100644 --- a/src/Command/TestCommand.php +++ b/src/Command/TestCommand.php @@ -676,7 +676,7 @@ public function generateConstructor(string $type, string $fullClassName): array * @param string $type The Type of object you are generating tests for eg. controller * @param string $subject The name of the test subject. * @param string $fullClassName The Classname of the class the test is being generated for. - * @return array An array containing property info + * @return list An array containing property info */ public function generateProperties(string $type, string $subject, string $fullClassName): array { diff --git a/src/Utility/CommonOptionsTrait.php b/src/Utility/CommonOptionsTrait.php index 29541216..64ee4c85 100644 --- a/src/Utility/CommonOptionsTrait.php +++ b/src/Utility/CommonOptionsTrait.php @@ -71,7 +71,7 @@ protected function extractCommonProperties(Arguments $args): void /** * Get available bake themes * - * @return array + * @return list */ protected function _getBakeThemes(): array { diff --git a/src/Utility/Model/AssociationFilter.php b/src/Utility/Model/AssociationFilter.php index db3d1692..bf2011ef 100644 --- a/src/Utility/Model/AssociationFilter.php +++ b/src/Utility/Model/AssociationFilter.php @@ -61,7 +61,7 @@ public function belongsToManyJunctionAliases(Table $table): array * already existing in BelongsToMany * * @param \Cake\ORM\Table $model The model to build associations for. - * @return array associations + * @return array>> associations */ public function filterAssociations(Table $model): array { diff --git a/src/Utility/TemplateRenderer.php b/src/Utility/TemplateRenderer.php index 7566cd07..89f5360a 100644 --- a/src/Utility/TemplateRenderer.php +++ b/src/Utility/TemplateRenderer.php @@ -82,7 +82,7 @@ public function getView(): View * Runs the template * * @param string $template bake template to render - * @param array|null $vars Additional vars to set to template scope. + * @param array|null $vars Additional vars to set to template scope. * @return string contents of generated code template */ public function generate(string $template, ?array $vars = null): string diff --git a/src/View/BakeView.php b/src/View/BakeView.php index ebc623df..99a77010 100644 --- a/src/View/BakeView.php +++ b/src/View/BakeView.php @@ -105,7 +105,7 @@ public function render(?string $template = null, string|false|null $layout = nul * Use the Bake prefix for bake related view events * * @param string $name Name of the event. - * @param array $data Any value you wish to be transported with this event to + * @param array $data Any value you wish to be transported with this event to * it can be read by listeners. * @param object|null $subject The object that this event applies to * ($this by default). diff --git a/src/View/Helper/BakeHelper.php b/src/View/Helper/BakeHelper.php index e73388c6..3c7b01ae 100644 --- a/src/View/Helper/BakeHelper.php +++ b/src/View/Helper/BakeHelper.php @@ -44,7 +44,7 @@ class BakeHelper extends Helper * Used for generating formatted properties such as component and helper arrays * * @param string $name the name of the property - * @param array $value the array of values + * @param array $value the array of values * @param array $options extra options to be passed to the element * @return string */ @@ -89,7 +89,7 @@ public function exportVar(mixed $var, int $indentLevel = 0, int $options = 0): s * * (Similar to `var_export()` but better). * - * @param array $var Array to export. + * @param array $var Array to export. * @param int $indentLevel Identation level. * @param bool $inline Inline numeric scalar array (adds INLINE_NUMERIC_SCALAR_ARRAY flag) * @return string @@ -140,7 +140,7 @@ public function aliasExtractor(Table $table, string $assoc): array * @param string $class Class name * @param string $type Class type/sub-namespace * @param string $suffix Class name suffix - * @return array Class info + * @return array{fqn: string, namespace: string, class: string, plugin: ?string, name: string, fullName: string} Class info */ public function classInfo(string $class, string $type, string $suffix): array { @@ -181,12 +181,12 @@ public function hasPlugin(string $plugin): bool /** * Return list of fields to generate controls for. * - * @param array $fields Fields list. + * @param array $fields Fields list. * @param \Cake\Datasource\SchemaInterface $schema Schema instance. * @param \Cake\ORM\Table|null $modelObject Model object. * @param string|int $takeFields Take fields. * @param array $filterTypes Filter field types. - * @return array + * @return array */ public function filterFields( array $fields, @@ -216,10 +216,10 @@ public function filterFields( /** * Get fields data for view template. * - * @param array $fields Fields list. + * @param array $fields Fields list. * @param \Cake\Datasource\SchemaInterface $schema Schema instance. - * @param array $associations Associations data. - * @return array + * @param array> $associations Associations data. + * @return array{associationFields: mixed, groupedFields: array} */ public function getViewFieldsData(array $fields, SchemaInterface $schema, array $associations): array { @@ -287,7 +287,7 @@ public function getViewFieldsData(array $fields, SchemaInterface $schema, array * * @param string $field Field name. * @param \Cake\Database\Schema\TableSchema $schema Schema. - * @return array|null + * @return array|null */ public function columnData(string $field, TableSchema $schema): ?array { @@ -332,7 +332,7 @@ public function getAssociatedTableAlias(Table $modelObj, string $assoc): string * Get validation methods data. * * @param string $field Field name. - * @param array $rules Validation rules list. + * @param array> $rules Validation rules list. * @return array */ public function getValidationMethods(string $field, array $rules): array @@ -413,8 +413,8 @@ public function getFieldAccessibility(array|false|null $fields = null, ?array $p /** * Wrap string arguments with quotes * - * @param array $args array of arguments - * @return array + * @param array $args array of arguments + * @return array */ public function escapeArguments(array $args): array { diff --git a/src/View/Helper/DocBlockHelper.php b/src/View/Helper/DocBlockHelper.php index 014c3d9b..c94ed29a 100644 --- a/src/View/Helper/DocBlockHelper.php +++ b/src/View/Helper/DocBlockHelper.php @@ -103,8 +103,8 @@ public function associatedEntityTypeToHintType(string $type, Association $associ * ``` * * @see \Bake\Command\ModelCommand::getEntityPropertySchema - * @param array $propertySchema The property schema to use for generating the type map. - * @return array The property DocType map. + * @param array> $propertySchema The property schema to use for generating the type map. + * @return array The property DocType map. */ public function buildEntityPropertyHintTypeMap(array $propertySchema): array { @@ -140,8 +140,8 @@ public function buildEntityPropertyHintTypeMap(array $propertySchema): array * ``` * * @see \Bake\Command\ModelCommand::getEntityPropertySchema - * @param array $propertySchema The property schema to use for generating the type map. - * @return array The property DocType map. + * @param array> $propertySchema The property schema to use for generating the type map. + * @return array The property DocType map. */ public function buildEntityAssociationHintTypeMap(array $propertySchema): array { @@ -268,9 +268,9 @@ public function propertyHints(array $properties): array /** * Build property, method, mixing annotations for table class. * - * @param array $associations Associations list. - * @param array $associationInfo Association info. - * @param array $behaviors Behaviors list. + * @param array> $associations Associations list. + * @param array $associationInfo Association info. + * @param array> $behaviors Behaviors list. * @param string $entity Entity name. * @param string $namespace Namespace. * @return array @@ -324,10 +324,10 @@ public function buildTableAnnotations( * * In case the given key cannot be found, the value will be appended. * - * @param array $target The array in which to insert the new value. + * @param array $target The array in which to insert the new value. * @param string $key The array key after which to insert the new value. * @param mixed $value The entry to insert. - * @return array The array with the new value inserted. + * @return array The array with the new value inserted. */ protected function _insertAfter(array $target, string $key, mixed $value): array { diff --git a/tests/TestCase/Command/TemplateCommandTest.php b/tests/TestCase/Command/TemplateCommandTest.php index 03bacea7..4f6617d6 100644 --- a/tests/TestCase/Command/TemplateCommandTest.php +++ b/tests/TestCase/Command/TemplateCommandTest.php @@ -664,7 +664,7 @@ public function testBakeIndexPlugin(): void * * @return void */ - public function testBakePluginTemplatesWithDomain() + public function testBakePluginTemplatesWithDomain(): void { $this->_loadTestPlugin('BakeTest'); $path = Plugin::templatePath('BakeTest'); @@ -678,7 +678,7 @@ public function testBakePluginTemplatesWithDomain() $this->assertExitCode(CommandInterface::CODE_SUCCESS); $this->assertFileExists($this->generatedFile); - $this->assertFileContains('__d(\'BakeTest\', ', $this->generatedFile); + $this->assertFileContains("__d('BakeTest', ", $this->generatedFile); } /**