Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="phpstan" version="2.1.55" installed="2.1.55" location="./tools/phpstan" copy="false"/>
<phar name="phpstan" version="2.2.2" installed="2.2.2" location="./tools/phpstan" copy="false"/>
</phive>
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ parameters:
- src/
bootstrapFiles:
- tests/bootstrap.php
ignoreErrors:
- identifier: missingType.iterableValue
3 changes: 3 additions & 0 deletions src/CodeGen/CodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class CodeParser extends NodeVisitorAbstract

protected string $fileText = '';

/**
* @var array<string, mixed>
*/
protected array $parsed = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $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
Expand Down
2 changes: 1 addition & 1 deletion src/Command/EntryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $argv Arguments from the CLI environment.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null Exit code or null for success.
*/
Expand Down
16 changes: 8 additions & 8 deletions src/Command/FixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,19 @@ protected function _generateSchema(TableSchemaInterface $table): string
{
$cols = $indexes = $constraints = [];
foreach ($table->columns() as $field) {
/** @var array $fieldData */
/** @var array<string, mixed> $fieldData */
$fieldData = $table->getColumn($field);
$properties = implode(', ', $this->_values($fieldData));
$cols[] = " '{$field}' => [{$properties}],";
}
foreach ($table->indexes() as $index) {
/** @var array $fieldData */
/** @var array<string, mixed> $fieldData */
$fieldData = $table->getIndex($index);
$properties = implode(', ', $this->_values($fieldData));
$indexes[] = " '{$index}' => [{$properties}],";
}
foreach ($table->constraints() as $index) {
/** @var array $fieldData */
/** @var array<string, mixed> $fieldData */
$fieldData = $table->getConstraint($index);
$properties = implode(', ', $this->_values($fieldData));
$constraints[] = " '{$index}' => [{$properties}],";
Expand All @@ -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<string, mixed> $values options keys(type, null, default, key, length, extra)
* @return array<string> Formatted values
*/
protected function _values(array $values): array
Expand All @@ -351,15 +351,15 @@ 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-key, array<string, mixed>> Array of records to use in the fixture.
*/
protected function _generateRecords(TableSchemaInterface $table, int $recordCount = 1): array
{
$records = [];
for ($i = 0; $i < $recordCount; $i++) {
$record = [];
foreach ($table->columns() as $field) {
/** @var array $fieldInfo */
/** @var array<string, mixed> $fieldInfo */
$fieldInfo = $table->getColumn($field);
$insert = '';
switch ($fieldInfo['type']) {
Expand Down Expand Up @@ -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<array-key, mixed> $records Array of records to be converted to string
* @return string A string value of the $records array.
* @throws \Brick\VarExporter\ExportException
*/
Expand All @@ -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-key, mixed> Array of records.
*/
protected function _getRecordsFromTable(Arguments $args, string $modelName, ?string $useTable = null): array
{
Expand Down
67 changes: 34 additions & 33 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
public function getTableContext(
Table $tableObject,
Expand Down Expand Up @@ -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<string, array<int|string, mixed>>
*/
public function getAssociations(Table $table, Arguments $args, ConsoleIo $io): array
{
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations The associations to append.
* @return void
*/
public function applyAssociations(Table $model, array $associations): void
Expand Down Expand Up @@ -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<string, array{targetFqn: string}> A map of association information.
*/
public function getAssociationInfo(Table $table): array
{
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations Array of in progress associations
* @param \Cake\Console\Arguments|null $args CLI arguments
* @return array Associations with belongsTo added in.
* @return array<string, array<int|string, mixed>> Associations with belongsTo added in.
*/
public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array
{
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations Array of in progress associations
* @return array<string, array<int|string, mixed>> Associations with hasOne added in.
*/
public function findHasOne(Table $model, array $associations): array
{
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations Array of in progress associations
* @return array<string, array<int|string, mixed>> Associations with hasMany added in.
*/
public function findHasMany(Table $model, array $associations): array
{
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations Array of in-progress associations
* @return array<string, array<int|string, mixed>> Associations with belongsToMany added in.
*/
public function findBelongsToMany(Table $model, array $associations): array
{
Expand Down Expand Up @@ -691,15 +691,15 @@ 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<string, array<string, mixed>> The property schema
*/
public function getEntityPropertySchema(Table $model): array
{
$properties = [];

$schema = $model->getSchema();
foreach ($schema->columns() as $column) {
/** @var array $columnSchema */
/** @var array<string, mixed> $columnSchema */
$columnSchema = $schema->getColumn($column);

$properties[$column] = [
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations The associations list.
* @param \Cake\Console\Arguments $args CLI Arguments
* @return array|false The validation rules.
* @return array<string, array<string, mixed>>|false The validation rules.
*/
public function getValidation(Table $model, array $associations, Arguments $args): array|false
{
Expand Down Expand Up @@ -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<string, mixed> $metaData metadata for field
* @param array<string> $primaryKey The primary key field. Unused because PK validation is skipped
* @return array Array of validation for the field.
* @return array<string, array<string, mixed>> Array of validation for the field.
*/
public function fieldValidation(
TableSchemaInterface $schema,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<string, mixed> $metaData Field meta data.
* @param string $prefix Method name prefix.
* @return string
*/
Expand Down Expand Up @@ -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<string, array<int|string, mixed>> $associations The associations for the model.
* @param \Cake\Console\Arguments $args CLI Arguments
* @return array The rules to be applied.
* @return list<array<string, mixed>> The rules to be applied.
*/
public function getRules(Table $model, array $associations, Arguments $args): array
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<string, array<mixed>> Behaviors
*/
public function getBehaviors(Table $model): array
{
Expand Down Expand Up @@ -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<string, array> CounterCache configurations
* @return array<string, list<string>> CounterCache configurations
*/
public function getCounterCache(Table $model): array
{
Expand Down Expand Up @@ -1472,7 +1473,7 @@ protected function possibleEnumFields(TableSchemaInterface $schema): array
$fields = [];

foreach ($schema->columns() as $column) {
/** @var array $columnSchema */
/** @var array<string, mixed> $columnSchema */
$columnSchema = $schema->getColumn($column);
if (str_starts_with((string)$columnSchema['type'], 'enum-')) {
$fields[] = $column;
Expand All @@ -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;
}
Expand Down Expand Up @@ -1576,8 +1577,8 @@ protected function bakeEnums(Table $model, array $data, Arguments $args, Console
}

/**
* @param array<string, array<string, mixed>> $associations
* @return array<string, array<string, mixed>>
* @param array<string, array<int|string, mixed>> $associations
* @return array<string, array<int|string, mixed>>
*/
protected function ensureAliasUniqueness(array $associations): array
{
Expand Down
17 changes: 12 additions & 5 deletions src/Command/TemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> Returns variables to be made available to a view template
*/
protected function _loadController(ConsoleIo $io): array
{
Expand Down Expand Up @@ -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'];
}
Expand Down Expand Up @@ -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<string, mixed>|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
Expand Down Expand Up @@ -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}");
}

/**
Expand Down Expand Up @@ -450,7 +457,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
* To be mocked...
*
* @param \Cake\ORM\Table $model Table
* @return array associations
* @return array<string, array<string, mixed>> associations
*/
protected function _filteredAssociations(Table $model): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{description?: string, type: string, name: string}> An array containing property info
*/
public function generateProperties(string $type, string $subject, string $fullClassName): array
{
Expand Down
Loading
Loading