Skip to content

Commit

Permalink
Merge pull request #8072 from kenjis/declare_strict_types
Browse files Browse the repository at this point in the history
refactor: add `declare(strict_types=1)`
  • Loading branch information
kenjis authored Dec 4, 2023
2 parents f9679ec + 5307da0 commit d61e33e
Show file tree
Hide file tree
Showing 845 changed files with 1,708 additions and 24 deletions.
7 changes: 3 additions & 4 deletions contributing/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ PHP7 provides [Type declarations](https://www.php.net/manual/en/language.types.d
for method parameters and return types. Use it where possible. Return type
declaration is not always practical, but do try to make it work.

At this time, shipped CI4 production code does not use
[Strict typing](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict),
and will not be any time soon. However, in the development phase,
there are internal classes (in `utils/`) that are strictly typed.
At this time, shipped CI4 production code does use
[Strict typing](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict)
as much as possible.

## Abstractions

Expand Down
17 changes: 17 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down Expand Up @@ -43,6 +45,7 @@
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
use Utils\Rector\RemoveErrorSuppressInTryCatchStmtsRector;
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
Expand Down Expand Up @@ -106,6 +109,19 @@
__DIR__ . '/system/Session/Handlers',
],

DeclareStrictTypesRector::class => [
__DIR__ . '/app',
__DIR__ . '/system/CodeIgniter.php',
__DIR__ . '/system/Config/BaseConfig.php',
__DIR__ . '/system/Commands/Generators/Views',
__DIR__ . '/system/Pager/Views',
__DIR__ . '/system/Test/ControllerTestTrait.php',
__DIR__ . '/system/Validation/Views',
__DIR__ . '/system/View/Parser.php',
__DIR__ . '/tests/system/Debug/ExceptionsTest.php',
__DIR__ . '/tests/system/View/Views',
],

// use mt_rand instead of random_int on purpose on non-cryptographically random
RandomFunctionRector::class,

Expand All @@ -116,6 +132,7 @@
$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();

$rectorConfig->rule(DeclareStrictTypesRector::class);
$rectorConfig->rule(UnderscoreToCamelCaseVariableNameRector::class);
$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveAlwaysElseRector::class);
Expand Down
2 changes: 2 additions & 0 deletions system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Autoloader/FileLocatorCached.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Autoloader/FileLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/Commands.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/Console.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/Exceptions/CLIException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Exceptions/CacheException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Exceptions/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/FactoriesCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/FactoriesCache/FileVarExportHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/DummyHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ protected function runController($class)
// This is a Web request or PHP CLI request
$params = $this->router->params();

// The controller method param types may not be string.
// So cannot set `declare(strict_types=1)` in this file.
$output = method_exists($class, '_remap')
? $class->_remap($this->method, ...$params)
: $class->{$this->method}(...$params);
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Cache/ClearCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Cache/InfoCache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/CreateDatabase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/Migrate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/MigrateRefresh.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/MigrateRollback.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/MigrateStatus.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/Seed.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Database/ShowTableInfo.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Encryption/GenerateKey.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Generators/CommandGenerator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Generators/ConfigGenerator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
2 changes: 2 additions & 0 deletions system/Commands/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
Expand Down
Loading

0 comments on commit d61e33e

Please sign in to comment.