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

6.x: add ruleset for CakePHP 6.0 + accessible => patchable config #306

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.1",
"cakephp/console": "^5.0",
"cakephp/cakephp": "^5.0",
LordSimal marked this conversation as resolved.
Show resolved Hide resolved
LordSimal marked this conversation as resolved.
Show resolved Hide resolved
"nette/utils": "^4.0",
"rector/rector": "~2.0.0",
"symfony/string": "^6.0 || ^7.0"
Expand Down
9 changes: 9 additions & 0 deletions config/rector/cakephp60.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

use Cake\Upgrade\Rector\Set\CakePHPSetList;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([CakePHPSetList::CAKEPHP_60]);
};
26 changes: 26 additions & 0 deletions config/rector/sets/cakephp60.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\Rector\PropertyFetch\RenamePropertyRector;
use Rector\Renaming\Rector\String_\RenameStringRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameProperty;

# @see https://book.cakephp.org/6/en/appendices/6-0-migration-guide.html
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
new MethodCallRename('Cake\ORM\Entity', 'setAccess', 'setPatchable'),
new MethodCallRename('Cake\ORM\Entity', 'getAccessible', 'getPatchable'),
new MethodCallRename('Cake\ORM\Entity', 'isAccessible', 'isPatchable'),
]);

$rectorConfig->ruleWithConfiguration(RenamePropertyRector::class, [
new RenameProperty('Cake\ORM\Entity', '_accessible', 'patchable'),
]);

$rectorConfig->ruleWithConfiguration(RenameStringRector::class, [
'accessibleFields' => 'patchableFields',
]);
};
5 changes: 5 additions & 0 deletions src/Rector/Set/CakePHPSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ final class CakePHPSetList
*/
public const CAKEPHP_52 = __DIR__ . '/../../../config/rector/sets/cakephp52.php';

/**
* @var string
*/
public const CAKEPHP_60 = __DIR__ . '/../../../config/rector/sets/cakephp60.php';

/**
* @var string
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase/Command/RectorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ public function testApply52()
$this->assertTestAppUpgraded();
}

public function testApply60()
{
$this->setupTestApp(__FUNCTION__);
$this->exec('upgrade rector --rules cakephp60 ' . TEST_APP);
$this->assertTestAppUpgraded();
}

public function testApplyMigrations45()
{
$this->setupTestApp(__FUNCTION__);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Category extends Entity
{
protected array $_accessible = [];

public function setAccess(array|string $field, bool $set) {
}

public function getAccessible(): array {
return [];
}

public function isAccessible(string $field): bool {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

class ORMMethods
{
public function test()
{
$table = new Cake\ORM\Table();

$table->newEntity([], [
'associated' => [
'Articles' => [
'accessibleFields' => ['title', 'body']
]
],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace App\Model\Entity;

use Cake\ORM\Entity;

class Category extends Entity
{
protected array $patchable = [];

public function setPatchable(array|string $field, bool $set) {
}

public function getPatchable(): array {
return [];
}

public function isPatchable(string $field): bool {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

class ORMMethods
{
public function test()
{
$table = new Cake\ORM\Table();

$table->newEntity([], [
'associated' => [
'Articles' => [
'patchableFields' => ['title', 'body']
]
],
]);
}
}
Loading