Skip to content

Commit d0e17bd

Browse files
authored
Merge pull request #11 from samsonasik/refactor-enable-rector-and-phpstan-level-6
refactor: enable Rector for code analyze and increase PHPStan to level 6
2 parents b44e678 + 1358af6 commit d0e17bd

File tree

10 files changed

+42
-15
lines changed

10 files changed

+42
-15
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.gitattributes export-ignore
33
/.php-cs-fixer.dist.php export-ignore
44
/phpstan.neon export-ignore
5+
/rector.php export-ignore

.github/workflows/tests_build.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ jobs:
3030
- name: "CS Check"
3131
run: "vendor/bin/php-cs-fixer fix --dry-run --diff"
3232
- name: "Code analyze"
33-
run: "vendor/bin/phpstan"
33+
run: |
34+
vendor/bin/phpstan
35+
vendor/bin/rector process --dry-run
3436
- name: "Run test suite"
3537
run: "vendor/bin/phpunit"

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"require-dev": {
2828
"codeigniter/coding-standard": "^1.8",
2929
"phpunit/phpunit": "^10.5",
30-
"phpstan/phpstan": "^2.1"
30+
"phpstan/phpstan": "^2.1",
31+
"rector/rector": "^2.0"
3132
}
3233
}

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ parameters:
22
paths:
33
- src
44
- tests
5-
level: 5
5+
level: 6
66
bootstrapFiles:
77
- vendor/codeigniter4/framework/system/Test/bootstrap.php
88

rector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return RectorConfig::configure()
8+
->withPaths([
9+
__DIR__ . '/src',
10+
__DIR__ . '/tests',
11+
])
12+
->withPhpSets()
13+
->withPreparedSets(deadCode: true, codeQuality: true)
14+
->withImportNames();

src/Commands/PageCreateCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ class PageCreateCommand extends BaseCommand
2020
protected $name = 'page:create'; // Nama command
2121
protected $description = 'Create a new page router';
2222

23+
/**
24+
* @param list<string> $params
25+
*/
2326
public function run(array $params)
2427
{
25-
if (empty($params)) {
28+
if ($params === []) {
2629
CLI::error('Please specify the page name.');
2730

2831
return;
@@ -67,6 +70,10 @@ public function run(array $params)
6770

6871
/**
6972
* Create a file from a template, replacing placeholders.
73+
*
74+
* @param array<string, string> $replacements
75+
*
76+
* @return void
7077
*/
7178
private function createFileFromTemplate(string $templateFile, string $targetFile, array $replacements)
7279
{

src/Config/Services.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
class Services extends BaseService
2121
{
22+
/**
23+
* @return PageRouter
24+
*/
2225
public static function router(?RouteCollectionInterface $routes = null, ?Request $request = null, bool $getShared = true)
2326
{
2427
if ($getShared) {

src/Helpers/pageview_helper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use Config\View;
1414

1515
if (! function_exists('pageView')) {
16+
/**
17+
* @param array<string, mixed> $data
18+
* @param array<string, mixed> $options
19+
*/
1620
function pageView(string $name, array $data = [], array $options = []): string
1721
{
1822
$config = config(View::class);
@@ -45,13 +49,11 @@ function asset_url(string $filePath): string
4549
$fullFilePath = FCPATH . $filePath;
4650

4751
// Check if file exists
48-
if (file_exists($fullFilePath)) {
52+
$version = file_exists($fullFilePath)
4953
// Add file modification time as version
50-
$version = filemtime($fullFilePath);
51-
} else {
54+
? filemtime($fullFilePath)
5255
// Fallback version (current timestamp if file doesn't exist)
53-
$version = time();
54-
}
56+
: time();
5557

5658
// Generate full URL with version
5759
return base_url($filePath) . '?v=' . $version;

src/PageRouter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
use CodeIgniter\Exceptions\PageNotFoundException;
1818
use CodeIgniter\HTTP\Exceptions\BadRequestException;
1919
use CodeIgniter\HTTP\ResponseInterface;
20-
use CodeIgniter\Router\RouteCollection;
2120
use CodeIgniter\Router\Router;
2221

2322
/**
2423
* Page based router.
2524
*
2625
* This class extends the CodeIgniter's Router class
2726
* to handle page based routes.
28-
*
29-
* @property-read RouteCollection $collection
3027
*/
3128
class PageRouter extends Router
3229
{
@@ -71,7 +68,7 @@ public function pageBasedRoute(string $uri)
7168
$uri = trim($uri, '/');
7269

7370
// Set default page for root uri
74-
if (empty($uri)) {
71+
if ($uri === '' || $uri === '0') {
7572
$uri = config('App')->defaultPage ?? 'home';
7673
}
7774

@@ -83,7 +80,7 @@ public function pageBasedRoute(string $uri)
8380

8481
$uriSegments = explode('/', $uri);
8582

86-
while (count($uriSegments) > 0) {
83+
while ($uriSegments !== []) {
8784
$folderPath = $pagesPath . '/' . str_replace('/', DIRECTORY_SEPARATOR, implode('/', $uriSegments));
8885
if (is_dir($folderPath) && file_exists($folderPath . '/' . $controllerName . '.php')) {
8986
$uri = implode('/', $uriSegments);

tests/Helpers/PageViewTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
final class PageViewTest extends TestCase
1919
{
20-
public function testPageView()
20+
public function testPageView(): void
2121
{
2222
helper('Yllumi\Ci4Pages\Helpers\pageview');
2323

0 commit comments

Comments
 (0)