Skip to content

Commit 6f5c1d7

Browse files
authored
style: implement CodeIgniterRuleCustomisationPolicy (codeigniter4#9859)
1 parent f77d6b8 commit 6f5c1d7

File tree

5 files changed

+69
-17
lines changed

5 files changed

+69
-17
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
'ThirdParty',
2727
'Validation/Views',
2828
])
29-
->notName('#Foobar.php$#')
3029
->append([
3130
__FILE__,
3231
__DIR__ . '/.php-cs-fixer.no-header.php',
@@ -37,9 +36,7 @@
3736
__DIR__ . '/spark',
3837
]);
3938

40-
$overrides = [
41-
'modernize_strpos' => ['modernize_stripos' => true],
42-
];
39+
$overrides = [];
4340

4441
$options = [
4542
'cacheFile' => 'build/.php-cs-fixer.cache',

.php-cs-fixer.tests.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14-
use PhpCsFixer\ConfigInterface;
14+
use PhpCsFixer\Config;
1515
use PhpCsFixer\Finder;
16+
use Utils\PhpCsFixer\CodeIgniterRuleCustomisationPolicy;
1617

17-
/** @var ConfigInterface $config */
18+
/** @var Config $config */
1819
$config = require __DIR__ . '/.php-cs-fixer.dist.php';
1920

2021
$finder = Finder::create()
@@ -26,16 +27,15 @@
2627
'_support/View/Cells/multiplier.php',
2728
'_support/View/Cells/colors.php',
2829
'_support/View/Cells/addition.php',
29-
'system/Database/Live/PreparedQueryTest.php',
30-
])
31-
->notName('#Foobar.php$#');
30+
]);
3231

3332
$overrides = [
3433
'phpdoc_to_return_type' => true,
3534
'void_return' => true,
3635
];
3736

3837
return $config
38+
->setRuleCustomisationPolicy(new CodeIgniterRuleCustomisationPolicy())
3939
->setFinder($finder)
4040
->setCacheFile('build/.php-cs-fixer.tests.cache')
4141
->setRules(array_merge($config->getRules(), $overrides));

tests/_support/Commands/Foobar.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
<?php
22

3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
312
use Config\App;
413
use CodeIgniter\CLI\CLI;
514

615
return [
7-
'foo' => 'The command will use this as foo.',
8-
'bar' => 'The command will use this as bar.',
9-
'baz' => 'The baz is here.',
10-
'bas' => CLI::color('bas', 'green') . (new App())->baseURL,
16+
'foo' => 'The command will use this as foo.',
17+
'bar' => 'The command will use this as bar.',
18+
'baz' => 'The baz is here.',
19+
'bas' => CLI::color('bas', 'green') . (new App())->baseURL,
1120
];

tests/system/Database/Live/PreparedQueryTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,15 @@ public function testDeallocatePreparedQueryThenTryToClose(): void
256256
'name' => 'a',
257257
'email' => '[email protected]',
258258
'country' => 'x',
259-
])
259+
]),
260260
);
261261

262262
$this->query->close();
263263

264264
// Try to close a non-existing prepared statement
265265
$this->expectException(BadMethodCallException::class);
266266
$this->expectExceptionMessage(
267-
'Cannot call close on a non-existing prepared statement.'
267+
'Cannot call close on a non-existing prepared statement.',
268268
);
269269

270270
$this->query->close();
@@ -325,7 +325,7 @@ public function testHandleTransStatusMarksTransactionFailedDuringTransaction():
325325

326326
$this->seeInDatabase($this->db->DBPrefix . 'without_auto_increment', [
327327
'key' => 'test_key',
328-
'value' => 'test_value'
328+
'value' => 'test_value',
329329
]);
330330

331331
$this->assertFalse($this->query->execute('test_key', 'different_value'));
@@ -339,7 +339,7 @@ public function testHandleTransStatusMarksTransactionFailedDuringTransaction():
339339
// Verify the first insert was rolled back
340340
$this->dontSeeInDatabase($this->db->DBPrefix . 'without_auto_increment', [
341341
'key' => 'test_key',
342-
'value' => 'test_value'
342+
'value' => 'test_value',
343343
]);
344344

345345
$this->db->resetTransStatus();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Utils\PhpCsFixer;
15+
16+
use PhpCsFixer\Config\RuleCustomisationPolicyInterface;
17+
use SplFileInfo;
18+
19+
/**
20+
* Rule customisation policy for CodeIgniter coding standard.
21+
*
22+
* @internal
23+
*/
24+
final class CodeIgniterRuleCustomisationPolicy implements RuleCustomisationPolicyInterface
25+
{
26+
public function getPolicyVersionForCache(): string
27+
{
28+
return hash_file('sha256', __FILE__);
29+
}
30+
31+
public function getRuleCustomisers(): array
32+
{
33+
$normalisedStrEndsWith = static fn (string $haystack, string $needle): bool => str_ends_with(str_replace('\\', '/', $haystack), $needle);
34+
35+
return [
36+
'native_function_casing' => static fn (SplFileInfo $file): bool => ! $normalisedStrEndsWith(
37+
$file->getPathname(),
38+
'/tests/system/Database/Live/PreparedQueryTest.php',
39+
),
40+
'ordered_imports' => static fn (SplFileInfo $file): bool => ! $normalisedStrEndsWith(
41+
$file->getPathname(),
42+
'/tests/_support/Commands/Foobar.php',
43+
),
44+
];
45+
}
46+
}

0 commit comments

Comments
 (0)