Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/improve type declarations for PHP 8.1 #2218

Merged
merged 4 commits into from
Sep 19, 2023
Merged

Add/improve type declarations for PHP 8.1 #2218

merged 4 commits into from
Sep 19, 2023

Conversation

othercorey
Copy link
Member

@othercorey othercorey commented Sep 15, 2023

cakephp-codesniffer 5 is meant to be paired with cakephp 5 code. One of the key changes is requiring native type declarations where possible.

@@ -6034,7 +6036,7 @@ public function testMigrationWithDropColumnAndForeignKeyAndIndex()
$adapter->disconnect();

$this->manager->setConfig($config);
$this->manager->migrate('production', '20190928205056');
$this->manager->migrate('production', 20190928205056);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests were passing string as a version instead of an int. This appears to only affect tests as the migration loading code converts to int.

@@ -40,7 +42,7 @@ public function __construct(Table $table, Column $column)
* @param array<string, mixed> $options The column options
* @return static
*/
public static function build(Table $table, string $columnName, $type = null, array $options = [])
public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to default to null only for a few tests which were omitting a type. This doesn't seem to be valid as the phinx code checks for either Literal or string. Updated tests to specify a type.

@@ -97,7 +103,7 @@ public function testConnectionWithSocketConnection()
}

$options = ['unix_socket' => getenv('MYSQL_UNIX_SOCKET')] + MYSQL_DB_CONFIG;
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG, new ArrayInput([]), new NullOutput());
$adapter = new MysqlAdapter($options, new ArrayInput([]), new NullOutput());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't passing the $config with socket configured so not sure if anything is running this test.

@othercorey othercorey force-pushed the cakephpcs-5 branch 2 times, most recently from a80eee8 to 7114e18 Compare September 15, 2023 05:43
* @return array
*/
protected function getIndexColums(int $tableId, int $indexId): array
protected function getIndexColums(string $tableId, string $indexId): array
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is passed strings not ints and is internal to this adapter so changed the types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unfortunate "quirk" of PDO in that it returns numerical columns as strings.

@MasterOdin MasterOdin changed the base branch from 1.x to 0.x September 19, 2023 08:04
@@ -55,7 +57,7 @@ public function __construct(Table $table, string $columnName, Column $column)
* @param array<string, mixed> $options Additional options for the column
* @return static
*/
public static function build(Table $table, string $columnName, $type = null, array $options = [])
public static function build(Table $table, string $columnName, string|Column|Literal $type, array $options = []): static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the docstring on this function was wrong, and similar to above, we should only accept string|Literal here as that's what setType accepts.

* @return array
*/
protected function getIndexColums(int $tableId, int $indexId): array
protected function getIndexColums(string $tableId, string $indexId): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unfortunate "quirk" of PDO in that it returns numerical columns as strings.

@MasterOdin MasterOdin changed the title Upgrade to cakephp-cs 5 and add type declarations Add/improve type declarations for PHP 8.1 Sep 19, 2023
@MasterOdin MasterOdin merged commit bd3f525 into 0.x Sep 19, 2023
10 of 12 checks passed
@MasterOdin MasterOdin deleted the cakephpcs-5 branch September 19, 2023 08:57
* @param array<string, mixed> $options Column Options
* @throws \InvalidArgumentException
* @return $this
*/
public function addColumn($columnName, $type = null, array $options = [])
public function addColumn(string|Column $columnName, string|Literal $type, array $options = [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it was possibly an accident? When supplying a Column instance in the first argument, it should already hold the type, right?

Error: src/Table.php:67:47: PossiblyNullArgument: Argument 2 of Phinx\Db\Table::addColumn cannot be null, possibly null value provided (see https://psalm.dev/078)

https://github.com/cakephp/migrations/actions/runs/6269091941/job/17025092704?pr=628

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrations support for phinx 0.15 is here cakephp/migrations#638

@othercorey
Copy link
Member Author

othercorey commented Oct 16, 2023

@MasterOdin You can override the header settings for phinx from the cakephpcs defaults now if you want. I don't think there will be any large-scale changes after this.

You can also move away from cakephp-cs. It was useful for validating the type declarations and type-hints, but you could pull out the specific rules you want into a custom config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants