diff --git a/contributing/internals.md b/contributing/internals.md index e2d387028fe1..b49c9832d390 100644 --- a/contributing/internals.md +++ b/contributing/internals.md @@ -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 diff --git a/rector.php b/rector.php index 1eaacd12328b..4410fb2f9464 100644 --- a/rector.php +++ b/rector.php @@ -1,5 +1,7 @@ [ + __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, @@ -116,6 +132,7 @@ $rectorConfig->importNames(); $rectorConfig->removeUnusedImports(); + $rectorConfig->rule(DeclareStrictTypesRector::class); $rectorConfig->rule(UnderscoreToCamelCaseVariableNameRector::class); $rectorConfig->rule(SimplifyUselessVariableRector::class); $rectorConfig->rule(RemoveAlwaysElseRector::class); diff --git a/system/API/ResponseTrait.php b/system/API/ResponseTrait.php index 5a350007f3fa..b92a1b808a93 100644 --- a/system/API/ResponseTrait.php +++ b/system/API/ResponseTrait.php @@ -1,5 +1,7 @@ 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); diff --git a/system/Commands/Cache/ClearCache.php b/system/Commands/Cache/ClearCache.php index 79fab9dfae3c..e1180c28c6bd 100644 --- a/system/Commands/Cache/ClearCache.php +++ b/system/Commands/Cache/ClearCache.php @@ -1,5 +1,7 @@ config = $config; - if (! $this->config->hidden) { - throw HoneypotException::forNoHiddenValue(); - } - if (empty($this->config->container) || strpos($this->config->container, '{template}') === false) { $this->config->container = '
'; } diff --git a/system/HotReloader/DirectoryHasher.php b/system/HotReloader/DirectoryHasher.php index ba8d2bfbb891..0910f2fa94bc 100644 --- a/system/HotReloader/DirectoryHasher.php +++ b/system/HotReloader/DirectoryHasher.php @@ -1,5 +1,7 @@ controller->{$method}(...$params); } catch (Throwable $e) { $code = $e->getCode(); diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 1a2cbb0d61aa..8097c93d7a3e 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -1,5 +1,7 @@ config->filters[$filter]($replace, ...$param); } diff --git a/system/View/Plugins.php b/system/View/Plugins.php index 4b20caef8d3f..02a1bd4d3052 100644 --- a/system/View/Plugins.php +++ b/system/View/Plugins.php @@ -1,5 +1,7 @@ = $item ?> += $item ?> diff --git a/tests/_support/View/OtherCells/SampleClass.php b/tests/_support/View/OtherCells/SampleClass.php index cff88bc58c4f..be7fc380ee29 100644 --- a/tests/_support/View/OtherCells/SampleClass.php +++ b/tests/_support/View/OtherCells/SampleClass.php @@ -1,5 +1,7 @@ fixturesFolder, 2); - $dotenv->load(); - $this->assertSame('bar', getenv('FOO')); - $this->assertSame('baz', getenv('BAR')); - $this->assertSame('with spaces', getenv('SPACED')); - $this->assertSame('', getenv('NULL')); + $this->expectException(TypeError::class); + + new DotEnv($this->fixturesFolder, 2); } public function testCommentedLoadsVars(): void diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index 6f74414def4e..4b8c66c1e2e1 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -1,5 +1,7 @@ = 80100 ? null : 'random string'; try { + // We test DEPRECATED error, so cannot set `declare(strict_types=1)` in this file. strlen($maybeNull); $this->assertLogContains('error', '[DEPRECATED] strlen(): '); } catch (ErrorException $e) { diff --git a/tests/system/Debug/TimerTest.php b/tests/system/Debug/TimerTest.php index 2be069062d9c..659ccbda3359 100644 --- a/tests/system/Debug/TimerTest.php +++ b/tests/system/Debug/TimerTest.php @@ -1,5 +1,7 @@ assertTrue($this->honeypot->hasContent($this->request)); } - public function testConfigHidden(): void - { - $this->config->hidden = ''; - $this->expectException(HoneypotException::class); - $this->honeypot = new Honeypot($this->config); - } - public function testConfigTemplate(): void { $this->config->template = ''; diff --git a/tests/system/HotReloader/DirectoryHasherTest.php b/tests/system/HotReloader/DirectoryHasherTest.php index f5de5da523d5..5ec7d02f9974 100644 --- a/tests/system/HotReloader/DirectoryHasherTest.php +++ b/tests/system/HotReloader/DirectoryHasherTest.php @@ -1,5 +1,7 @@ extend('layout'); ?> +extend('layout'); ?> section('content'); ?>Second
diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index 2192e4841a5a..bccf8f440625 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -317,6 +317,7 @@ Changes :ref:`multiple-filters` are always enabled. - **RouteCollection:** The HTTP method keys in the protected property ``$routes`` has been fixed from lowercase to uppercase. +- ``declare(strict_types=1)`` has been added to most framework codebase. Deprecations ************