Skip to content
Open
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
78 changes: 0 additions & 78 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^PHPDoc tag @var with type string is not subtype of native type non\-falsy\-string\|true\.$#'
identifier: varTag.nativeType
count: 1
path: src/Command/BakeSeedCommand.php

-
message: '#^Strict comparison using \!\=\= between string and false will always evaluate to true\.$#'
identifier: notIdentical.alwaysTrue
count: 1
path: src/Command/BakeSeedCommand.php

-
message: '#^Parameter \#1 \$callback of function array_map expects \(callable\(int\|string\)\: mixed\)\|null, Closure\(string\)\: string given\.$#'
identifier: argument.type
count: 1
path: src/Db/Adapter/AbstractAdapter.php

-
message: '#^Unsafe usage of new static\(\)\.$#'
identifier: new.static
count: 1
path: src/Db/Adapter/AdapterFactory.php

-
message: '#^Offset ''id'' on non\-empty\-array\<string, mixed\> in isset\(\) always exists and is not nullable\.$#'
identifier: isset.offset
count: 2
path: src/Db/Adapter/MysqlAdapter.php

-
message: '#^Strict comparison using \!\=\= between Cake\\Database\\StatementInterface and null will always evaluate to true\.$#'
identifier: notIdentical.alwaysTrue
Expand All @@ -41,51 +11,3 @@ parameters:
identifier: method.notFound
count: 1
path: src/Db/Table/Index.php

-
message: '#^Call to an undefined method Migrations\\MigrationInterface\:\:down\(\)\.$#'
identifier: method.notFound
count: 1
path: src/Migration/Environment.php

-
message: '#^Call to an undefined method Migrations\\MigrationInterface\:\:up\(\)\.$#'
identifier: method.notFound
count: 1
path: src/Migration/Environment.php

-
message: '#^Call to function method_exists\(\) with Migrations\\MigrationInterface and ''useTransactions'' will always evaluate to true\.$#'
identifier: function.alreadyNarrowedType
count: 1
path: src/Migration/Environment.php

-
message: '#^Method Migrations\\Migration\\Manager\:\:getMigrationClassName\(\) should return class\-string\<Migrations\\MigrationInterface\> but returns string\.$#'
identifier: return.type
count: 2
path: src/Migration/Manager.php

-
message: '#^Parameter \#1 \.\.\.\$arg1 of function max expects non\-empty\-array, array given\.$#'
identifier: argument.type
count: 1
path: src/Migration/Manager.php

-
message: '#^Parameter \#3 \$length of function substr expects int\|null, int\<0, max\>\|false given\.$#'
identifier: argument.type
count: 1
path: src/Migration/Manager.php

-
message: '#^Offset 0 on non\-empty\-list\<string\> in isset\(\) always exists and is not nullable\.$#'
identifier: isset.offset
count: 2
path: src/Util/TableFinder.php

-
message: '#^Possibly invalid array key type Cake\\Database\\Schema\\TableSchemaInterface\|string\.$#'
identifier: offsetAccess.invalidOffset
count: 2
path: src/View/Helper/MigrationHelper.php
15 changes: 7 additions & 8 deletions src/Command/BakeSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ public function templateData(Arguments $arguments): array
if ($arguments->getOption('data')) {
$limit = (int)$arguments->getOption('limit');

/** @var string $fields */
$fields = $arguments->getOption('fields') ?: '*';
$fields = (string)$arguments->getOption('fields') ?: '*';
if ($fields !== '*') {
$fields = explode(',', $fields);
}
Expand Down Expand Up @@ -173,7 +172,7 @@ protected function bake(string $name, Arguments $args, ConsoleIo $io): void
* @param \Cake\Console\ConsoleOptionParser $parser Option parser to update.
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser = parent::buildOptionParser($parser);

Expand Down Expand Up @@ -214,6 +213,7 @@ protected function prettifyArray(array $array, int $tabCount = 3, string $indent
$lines = explode("\n", $content);

$inString = false;
$removeKeys = [];

foreach ($lines as $k => &$line) {
if ($k === 0) {
Expand All @@ -237,7 +237,7 @@ protected function prettifyArray(array $array, int $tabCount = 3, string $indent
$tabCount--;
} elseif (preg_match("/^\d+\s\=\>\s$/", $line)) {
// Mark '0 =>' kind of lines to remove
$line = false;
$removeKeys[] = $k;
continue;
}

Expand All @@ -264,10 +264,9 @@ protected function prettifyArray(array $array, int $tabCount = 3, string $indent
}
unset($line);

// Remove marked lines
$lines = array_filter($lines, function ($line): bool {
return $line !== false;
});
foreach ($removeKeys as $key) {
unset($lines[$key]);
}

return implode("\n", $lines);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/BakeSimpleMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected function getMigrationName(?string $name = null): string
* @param \Cake\Console\ConsoleOptionParser $parser Option parser to update.
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser = $this->_setCommonOptions($parser);

Expand Down
8 changes: 4 additions & 4 deletions src/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ public function bulkinsert(
* Generates the SQL for a bulk insert.
*
* @param \Migrations\Db\Table\TableMetadata $table The table to insert into
* @param array $rows The rows to insert
* @param array<int, array<string, mixed>> $rows The rows to insert
* @param \Migrations\Db\InsertMode|null $mode Insert mode
* @param array<string>|null $updateColumns Columns to update on upsert conflict
* @param array<string>|null $conflictColumns Columns that define uniqueness for upsert (unused in MySQL)
Expand All @@ -859,15 +859,15 @@ protected function generateBulkInsertSql(
$this->getInsertPrefix($mode),
$this->quoteTableName($table->getName()),
);
$current = current($rows);
$keys = array_keys($current);
$current = (array)current($rows);
$keys = array_map(strval(...), array_keys($current));

$sql .= '(' . implode(', ', array_map($this->quoteColumnName(...), $keys)) . ') VALUES ';

$upsertClause = $this->getUpsertClause($mode, $updateColumns, $conflictColumns);

if ($this->isDryRunEnabled()) {
$values = array_map(function ($row): string {
$values = array_map(function (array $row): string {
return '(' . implode(', ', array_map($this->quoteValue(...), $row)) . ')';
}, $rows);

Expand Down
7 changes: 7 additions & 0 deletions src/Db/Adapter/AdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class AdapterFactory
*/
protected static ?AdapterFactory $instance = null;

/**
* Constructor.
*/
final public function __construct()
{
}

/**
* Get the factory singleton instance.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ public function createTable(TableMetadata $table, array $columns = [], array $in
);

// Add the default primary key
if (!isset($options['id']) || (isset($options['id']) && $options['id'] === true)) {
if (!isset($options['id']) || $options['id'] === true) {
$options['id'] = 'id';
}

if (isset($options['id']) && is_string($options['id'])) {
if (is_string($options['id'])) {
$useUnsigned = (bool)Configure::read('Migrations.unsigned_primary_keys');
// Handle id => "field_name" to support AUTO_INCREMENT
$column = new Column();
Expand Down
7 changes: 2 additions & 5 deletions src/Migration/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function executeMigration(MigrationInterface $migration, string $directio
$migration->{MigrationInterface::INIT}();
}

$atomic = $adapter->hasTransactions();
if (method_exists($migration, 'useTransactions')) {
$atomic = $migration->useTransactions();
}
$atomic = $migration->useTransactions();
// begin the transaction if the adapter supports it
if ($atomic) {
$adapter->beginTransaction();
Expand All @@ -97,7 +94,7 @@ public function executeMigration(MigrationInterface $migration, string $directio
} else {
$migration->{MigrationInterface::CHANGE}();
}
} else {
} elseif (method_exists($migration, $direction)) {
$migration->{$direction}();
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,25 @@ public function markMigrated(int $version, string $path): bool
* Resolves a migration class name based on $path
*
* @param string $path Path to the migration file of which we want the class name
* @return class-string<\Migrations\MigrationInterface> Migration class name
* @return string Migration class name
* @phpstan-return class-string<\Migrations\MigrationInterface>
*/
protected function getMigrationClassName(string $path): string
{
$class = (string)preg_replace('/^\d+_/', '', basename($path));
$class = str_replace('_', ' ', $class);
$class = ucwords($class);
$class = str_replace(' ', '', $class);
if (str_contains($class, '.')) {
return substr($class, 0, strpos($class, '.'));

$dotPos = strpos($class, '.');
if ($dotPos !== false) {
/** @var class-string<\Migrations\MigrationInterface> $name */
$name = substr($class, 0, $dotPos);

return $name;
}

/** @var class-string<\Migrations\MigrationInterface> $class */
return $class;
}

Expand Down Expand Up @@ -448,7 +455,8 @@ public function migrate(?int $version = null, bool $fake = false, ?int $count =
}

if ($version === null) {
$version = max(array_merge($versions, array_keys($migrations)));
$candidates = [...$versions, ...array_keys($migrations)];
$version = $candidates ? max($candidates) : 0;
} elseif ($version !== 0 && !isset($migrations[$version])) {
$this->getIo()->out(sprintf(
'<comment>warning</comment> %s is not a valid version',
Expand Down
4 changes: 2 additions & 2 deletions src/Util/TableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getTablesToBake(CollectionInterface $collection, array $options

$config = (array)ConnectionManager::getConfig($this->connection);
$key = isset($config['schema']) ? 'schema' : 'database';
if (isset($split[0], $split[1]) && $config[$key] === $split[1]) {
if (isset($split[1]) && $config[$key] === $split[1]) {
$table = $split[0];
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public function fetchTableName(string $className, ?string $pluginName = null): a
$config = ConnectionManager::getConfig($this->connection);
if (is_array($config)) {
$key = isset($config['schema']) ? 'schema' : 'database';
if (isset($splitted[0]) && $config[$key] === $splitted[1]) {
if ($config[$key] === $splitted[1]) {
$tableName = $splitted[0];
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,7 @@ public function getCreateTablesElementData(array $tables): array
'tables' => [],
];
foreach ($tables as $table) {
$tableName = $table;
if ($table instanceof TableSchemaInterface) {
$tableName = $table->name();
}
$tableName = $table instanceof TableSchemaInterface ? $table->name() : $table;
$data = $this->getCreateTableData($table);
$tableConstraintsNoUnique = array_filter(
$data['constraints'],
Expand Down
3 changes: 1 addition & 2 deletions tests/TestCase/Migration/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ public function testExecutingAMigrationWithUseTransactions(): void
$adapterStub->expects($this->never())
->method('commitTransaction');

$adapterStub->expects($this->atLeastOnce())
->method('hasTransactions')
$adapterStub->method('hasTransactions')
->willReturn(true);

$this->environment->setAdapter($adapterStub);
Expand Down
Loading