diff --git a/structarmed.php b/structarmed.php index fc5c799b13c3..88932d17cdd0 100644 --- a/structarmed.php +++ b/structarmed.php @@ -10,7 +10,7 @@ * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ - +use Boundwize\StructArmed\Rule\Rules\Function_\MustHaveReturnTypeFunctionRule; use Boundwize\StructArmed\Preset\Presets\CodeQualityPreset; use CodeIgniter\Cache\ResponseCache; use CodeIgniter\HTTP\CLIRequest; @@ -52,6 +52,10 @@ ]) ->cacheDirectory(is_dir('/tmp') ? '/tmp/structarmed' : null) ->withPresets(Preset::PSR4(), Preset::CODEQUALITY()) + + ->layer('Helpers', __DIR__ . '/system/Helpers') + ->rule('helpers.functions_must_have_return_type', new MustHaveReturnTypeFunctionRule('Helpers')) + // Resolve CodeIgniter layers from class names because several layers share directories. ->layerPattern('API', '/^CodeIgniter\\\\API\\\\.*$/') ->layerPattern('Cache', '/^CodeIgniter\\\\Cache\\\\.*$/') @@ -110,7 +114,7 @@ 'Security' => ['Cookie', 'HTTP', 'I18n', 'Session'], 'Session' => ['Cookie', 'Database', 'HTTP', 'I18n'], 'Throttle' => ['Cache', 'I18n'], - 'Validation' => ['Database', 'HTTP'], + 'Validation' => ['Database', 'HTTP', 'Helpers'], 'View' => ['Cache'], ]) ->skipPathsForRuleset(['*test*']) diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index f0ea0637c545..c5ccde761544 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -21,10 +21,8 @@ * wildcard searches, like foo.*.bar * * @param array $array - * - * @return mixed */ - function dot_array_search(string $index, array $array) + function dot_array_search(string $index, array $array): mixed { return ArrayHelper::dotSearch($index, $array); } @@ -36,10 +34,8 @@ function dot_array_search(string $index, array $array) * * @param int|string $key * @param array $array - * - * @return mixed */ - function array_deep_search($key, array $array) + function array_deep_search($key, array $array): mixed { if (isset($array[$key])) { return $array[$key]; diff --git a/system/Helpers/cookie_helper.php b/system/Helpers/cookie_helper.php index e3b10a1b60f6..3d3758278b8d 100644 --- a/system/Helpers/cookie_helper.php +++ b/system/Helpers/cookie_helper.php @@ -79,7 +79,7 @@ function set_cookie( * * @see \CodeIgniter\HTTP\IncomingRequest::getCookie() */ - function get_cookie($index, bool $xssClean = false, ?string $prefix = '') + function get_cookie($index, bool $xssClean = false, ?string $prefix = ''): array|string|null { if ($prefix === '') { $cookie = config(CookieConfig::class); diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index f9a04dd142df..7c42bd5c65f8 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -326,7 +326,7 @@ function get_dir_file_info(string $sourceDir, bool $topLevelOnly = true, bool $r * fileperms?: int * }|null */ - function get_file_info(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']) + function get_file_info(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): ?array { if (! is_file($file)) { return null; diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 1cf120acb78a..dee1f001365b 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -558,7 +558,7 @@ function form_close(string $extra = ''): string * * @return list|string */ - function set_value(string $field, $default = '', bool $htmlEscape = true) + function set_value(string $field, $default = '', bool $htmlEscape = true): array|string { $request = service('request'); @@ -695,7 +695,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st * * @return array */ - function validation_errors() + function validation_errors(): array { $errors = session('_ci_validation_errors'); diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 9f0b86470aa8..67441f935c11 100644 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -540,10 +540,8 @@ function embed(string $src, string $type = 'unknown', string $attributes = '', b if (! function_exists('_has_protocol')) { /** * Test the protocol of a URI. - * - * @return false|int */ - function _has_protocol(string $url) + function _has_protocol(string $url): false|int { return preg_match('#^([a-z]+:)?//#i', $url); } diff --git a/system/Helpers/kint_helper.php b/system/Helpers/kint_helper.php index 02d180328e92..e311d9ff7cc8 100644 --- a/system/Helpers/kint_helper.php +++ b/system/Helpers/kint_helper.php @@ -39,10 +39,8 @@ function dd(...$vars): void * dd function * * @param mixed ...$vars - * - * @return int */ - function dd(...$vars) + function dd(...$vars): int { return 0; } @@ -55,10 +53,8 @@ function dd(...$vars) * d function * * @param mixed ...$vars - * - * @return int */ - function d(...$vars) + function d(...$vars): int { return 0; } @@ -77,10 +73,8 @@ function trace(): void } else { /** * Generic trace function in case that Kint is not loaded. - * - * @return int */ - function trace() + function trace(): int { return 0; } diff --git a/system/Helpers/number_helper.php b/system/Helpers/number_helper.php index a737428706bd..2f4f6f9bed74 100644 --- a/system/Helpers/number_helper.php +++ b/system/Helpers/number_helper.php @@ -21,10 +21,8 @@ * * @param float|int|string $num Will be cast as int * @param non-empty-string|null $locale [optional] - * - * @return bool|string */ - function number_to_size($num, int $precision = 1, ?string $locale = null) + function number_to_size($num, int $precision = 1, ?string $locale = null): bool|string { try { // Strip any formatting & ensure numeric input @@ -76,10 +74,8 @@ function number_to_size($num, int $precision = 1, ?string $locale = null) * @param int|string $num Will be cast as int * @param int $precision [optional] The optional number of decimal digits to round to. * @param non-empty-string|null $locale [optional] - * - * @return bool|string */ - function number_to_amount($num, int $precision = 0, ?string $locale = null) + function number_to_amount($num, int $precision = 0, ?string $locale = null): bool|string { // Strip any formatting & ensure numeric input try { diff --git a/system/Helpers/test_helper.php b/system/Helpers/test_helper.php index b455aa301e2f..b8cbe0462350 100644 --- a/system/Helpers/test_helper.php +++ b/system/Helpers/test_helper.php @@ -28,7 +28,7 @@ * * @return array|object */ - function fake($model, ?array $overrides = null, $persist = true) + function fake($model, ?array $overrides = null, $persist = true): array|object { $fabricator = new Fabricator($model); @@ -49,10 +49,8 @@ function fake($model, ?array $overrides = null, $persist = true) * Used within our test suite to mock certain system tools. * * @param string $className Fully qualified class name - * - * @return object */ - function mock(string $className) + function mock(string $className): object { $mockClass = $className::$mockClass; $mockService = $className::$mockServiceName ?? ''; diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index f8ea0c15447c..cf9459100dfb 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -458,7 +458,7 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis * * @return array|string string or array */ - function strip_slashes($str) + function strip_slashes($str): array|string { if (! is_array($str)) { return stripslashes($str); diff --git a/system/Helpers/url_helper.php b/system/Helpers/url_helper.php index 884354a1254e..e43954909d65 100644 --- a/system/Helpers/url_helper.php +++ b/system/Helpers/url_helper.php @@ -71,7 +71,7 @@ function base_url($relativePath = '', ?string $scheme = null): string * @return string|URI When returning string, the query and fragment parts are removed. * When returning URI, the query and fragment parts are preserved. */ - function current_url(bool $returnObject = false, ?IncomingRequest $request = null) + function current_url(bool $returnObject = false, ?IncomingRequest $request = null): string|URI { $request ??= service('request'); /** @var CLIRequest|IncomingRequest $request */ @@ -87,10 +87,8 @@ function current_url(bool $returnObject = false, ?IncomingRequest $request = nul * we first check in a saved session variable, if it exists, and use that. * If that's not available, however, we'll use a sanitized url from $_SERVER['HTTP_REFERER'] * which can be set by the user so is untrusted and not set by certain browsers/servers. - * - * @return string|URI */ - function previous_url(bool $returnObject = false) + function previous_url(bool $returnObject = false): string|URI { // Grab from the session first, if we have it, // since it's more reliable and safer.