diff --git a/.github/workflows/rector-cs.yml b/.github/workflows/rector-cs.yml new file mode 100644 index 000000000..6424c2aab --- /dev/null +++ b/.github/workflows/rector-cs.yml @@ -0,0 +1,27 @@ +name: Rector + PHP CS Fixer + +on: + pull_request_target: + paths: + - 'src/**' + - 'tests/**' + - '.github/workflows/rector-cs.yml' + - 'composer.json' + - 'rector.php' + - '.php-cs-fixer.dist.php' + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector-cs.yml@master + secrets: + token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + php: '8.1' diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml deleted file mode 100644 index bb33197b6..000000000 --- a/.github/workflows/rector.yml +++ /dev/null @@ -1,24 +0,0 @@ -on: - pull_request_target: - paths-ignore: - - 'docs/**' - - 'README.md' - - 'CHANGELOG.md' - - '.gitignore' - - '.gitattributes' - - 'infection.json.dist' - - 'psalm.xml' - -name: rector - -jobs: - rector: - uses: yiisoft/actions/.github/workflows/rector.yml@master - secrets: - token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - os: >- - ['ubuntu-latest'] - php: >- - ['8.1'] diff --git a/.gitignore b/.gitignore index 60a33a6bb..81485c477 100644 --- a/.gitignore +++ b/.gitignore @@ -34,11 +34,6 @@ composer.phar /phpunit.xml /.phpunit.cache -# ignore dev installed apps and extensions -/apps -/extensions -/packages - -# NPM packages -/node_modules -.env +# PHP CS Fixer +/.php-cs-fixer.cache +/.php-cs-fixer.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 000000000..2477e66d8 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,21 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return ConfigBuilder::build() + ->setRiskyAllowed(true) + ->setParallelConfig(ParallelConfigFactory::detect()) + ->setRules([ + '@Yiisoft/Core' => true, + '@Yiisoft/Core:risky' => true, + ]) + ->setFinder($finder); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 1ab379b46..000000000 --- a/.styleci.yml +++ /dev/null @@ -1,85 +0,0 @@ -preset: psr12 -risky: true - -version: 8.1 - -finder: - exclude: - - docs - - vendor - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - hash_to_slash_comment - - is_null - - logical_operators - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals - - empty_loop_body_braces - - integer_literal_case - - union_type_without_spaces - -disabled: - - function_declaration diff --git a/CHANGELOG.md b/CHANGELOG.md index 688878c60..af7a8cde0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Bug #295: Remove unnecessary `CacheKeyNormalizer` instance creation in `CachedContent` (@samdark) - Enh #295: Minor refactor `ViewTrait::getParameter()` and `ViewTrait::resolveViewFilePath()` (@samdark) - Bug #300: Fix renderer selection for view files with double extensions (@vjik) +- Enh #302: Explicitly import classes, functions, and constants in "use" section (@vjik) ## 12.2.2 December 07, 2025 diff --git a/composer.json b/composer.json index 54e549c7f..6866cf98b 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "rector/rector": "^2.3.4", "spatie/phpunit-watcher": "^1.24.4", "yiisoft/aliases": "^3.1.1", + "yiisoft/code-style": "^1.0", "yiisoft/di": "^1.4.1", "yiisoft/psr-dummy-provider": "^1.0.2", "yiisoft/test-support": "^3.1.0" @@ -83,6 +84,7 @@ }, "scripts": { "bench": "phpbench run", + "cs-fix": "php-cs-fixer fix", "infection": "infection --threads=max", "test": "phpunit --testdox", "test-watch": "phpunit-watcher watch" diff --git a/src/Cache/CachedContent.php b/src/Cache/CachedContent.php index 135e0c0e3..c360d051b 100644 --- a/src/Cache/CachedContent.php +++ b/src/Cache/CachedContent.php @@ -40,7 +40,7 @@ public function __construct( private readonly string $id, private readonly CacheInterface $cache, array $dynamicContents = [], - array $variations = [] + array $variations = [], ) { $this->setDynamicContents($dynamicContents); $this->setVariations($variations); @@ -61,8 +61,8 @@ public function __construct( public function cache( string $content, DateInterval|int|null $ttl = 60, - Dependency|null $dependency = null, - float $beta = 1.0 + ?Dependency $dependency = null, + float $beta = 1.0, ): string { /** @psalm-suppress MixedArgument */ return $this->replaceDynamicPlaceholders( diff --git a/src/Cache/DynamicContent.php b/src/Cache/DynamicContent.php index 31dec3782..c69f53b6b 100644 --- a/src/Cache/DynamicContent.php +++ b/src/Cache/DynamicContent.php @@ -22,7 +22,7 @@ final class DynamicContent public function __construct( private readonly string $id, callable $contentGenerator, - private readonly array $parameters = [] + private readonly array $parameters = [], ) { $this->contentGenerator = $contentGenerator; } diff --git a/src/Event/View/AfterRender.php b/src/Event/View/AfterRender.php index f7233d387..85b699ee1 100644 --- a/src/Event/View/AfterRender.php +++ b/src/Event/View/AfterRender.php @@ -20,9 +20,8 @@ public function __construct( private readonly View $view, private readonly string $file, private readonly array $parameters, - private readonly string $result - ) { - } + private readonly string $result, + ) {} public function getView(): View { diff --git a/src/Event/View/BeforeRender.php b/src/Event/View/BeforeRender.php index f9660f8d7..3e3e7a7d6 100644 --- a/src/Event/View/BeforeRender.php +++ b/src/Event/View/BeforeRender.php @@ -21,9 +21,8 @@ final class BeforeRender implements StoppableEventInterface public function __construct( private readonly View $view, private readonly string $file, - private readonly array $parameters - ) { - } + private readonly array $parameters, + ) {} public function stopPropagation(): void { diff --git a/src/Event/View/PageBegin.php b/src/Event/View/PageBegin.php index 92ab85365..0277bfda3 100644 --- a/src/Event/View/PageBegin.php +++ b/src/Event/View/PageBegin.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\View; -final class PageBegin extends ViewEvent -{ -} +final class PageBegin extends ViewEvent {} diff --git a/src/Event/View/PageEnd.php b/src/Event/View/PageEnd.php index fa79d15e8..eda1459e4 100644 --- a/src/Event/View/PageEnd.php +++ b/src/Event/View/PageEnd.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\View; -final class PageEnd extends ViewEvent -{ -} +final class PageEnd extends ViewEvent {} diff --git a/src/Event/View/ViewEvent.php b/src/Event/View/ViewEvent.php index acf7e72d9..6f374c0ca 100644 --- a/src/Event/View/ViewEvent.php +++ b/src/Event/View/ViewEvent.php @@ -12,9 +12,8 @@ abstract class ViewEvent { final public function __construct( - private readonly View $view - ) { - } + private readonly View $view, + ) {} final public function getView(): View { diff --git a/src/Event/WebView/AfterRender.php b/src/Event/WebView/AfterRender.php index 2359572ce..ae9f9139c 100644 --- a/src/Event/WebView/AfterRender.php +++ b/src/Event/WebView/AfterRender.php @@ -20,9 +20,8 @@ public function __construct( private readonly WebView $view, private readonly string $file, private readonly array $parameters, - private readonly string $result - ) { - } + private readonly string $result, + ) {} public function getView(): WebView { diff --git a/src/Event/WebView/BeforeRender.php b/src/Event/WebView/BeforeRender.php index 038b62fd0..717398164 100644 --- a/src/Event/WebView/BeforeRender.php +++ b/src/Event/WebView/BeforeRender.php @@ -21,9 +21,8 @@ final class BeforeRender implements StoppableEventInterface public function __construct( private readonly WebView $view, private readonly string $file, - private readonly array $parameters - ) { - } + private readonly array $parameters, + ) {} public function stopPropagation(): void { diff --git a/src/Event/WebView/BodyBegin.php b/src/Event/WebView/BodyBegin.php index 2cf4e1b75..b5ee5dabc 100644 --- a/src/Event/WebView/BodyBegin.php +++ b/src/Event/WebView/BodyBegin.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\WebView; -final class BodyBegin extends WebViewEvent -{ -} +final class BodyBegin extends WebViewEvent {} diff --git a/src/Event/WebView/BodyEnd.php b/src/Event/WebView/BodyEnd.php index 0a187294c..fa06d2dcc 100644 --- a/src/Event/WebView/BodyEnd.php +++ b/src/Event/WebView/BodyEnd.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\WebView; -final class BodyEnd extends WebViewEvent -{ -} +final class BodyEnd extends WebViewEvent {} diff --git a/src/Event/WebView/Head.php b/src/Event/WebView/Head.php index fdd353a34..381bdfbe8 100644 --- a/src/Event/WebView/Head.php +++ b/src/Event/WebView/Head.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\WebView; -final class Head extends WebViewEvent -{ -} +final class Head extends WebViewEvent {} diff --git a/src/Event/WebView/PageBegin.php b/src/Event/WebView/PageBegin.php index 94bfd1cfb..a8365cbe8 100644 --- a/src/Event/WebView/PageBegin.php +++ b/src/Event/WebView/PageBegin.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\WebView; -final class PageBegin extends WebViewEvent -{ -} +final class PageBegin extends WebViewEvent {} diff --git a/src/Event/WebView/PageEnd.php b/src/Event/WebView/PageEnd.php index cf9cd0d12..5c9edc640 100644 --- a/src/Event/WebView/PageEnd.php +++ b/src/Event/WebView/PageEnd.php @@ -4,6 +4,4 @@ namespace Yiisoft\View\Event\WebView; -final class PageEnd extends WebViewEvent -{ -} +final class PageEnd extends WebViewEvent {} diff --git a/src/Event/WebView/WebViewEvent.php b/src/Event/WebView/WebViewEvent.php index 83f78d05d..daac4b4f9 100644 --- a/src/Event/WebView/WebViewEvent.php +++ b/src/Event/WebView/WebViewEvent.php @@ -12,9 +12,8 @@ abstract class WebViewEvent { final public function __construct( - private readonly WebView $view - ) { - } + private readonly WebView $view, + ) {} final public function getView(): WebView { diff --git a/src/Exception/ViewNotFoundException.php b/src/Exception/ViewNotFoundException.php index 55640e3bd..2f826475b 100644 --- a/src/Exception/ViewNotFoundException.php +++ b/src/Exception/ViewNotFoundException.php @@ -6,6 +6,4 @@ use Exception; -final class ViewNotFoundException extends Exception -{ -} +final class ViewNotFoundException extends Exception {} diff --git a/src/PhpTemplateRenderer.php b/src/PhpTemplateRenderer.php index d923bea9e..f74628a6b 100644 --- a/src/PhpTemplateRenderer.php +++ b/src/PhpTemplateRenderer.php @@ -14,6 +14,8 @@ use function ob_implicit_flush; use function ob_start; +use const EXTR_OVERWRITE; + /** * `PhpTemplateRenderer` renders the PHP views. */ diff --git a/src/State/LocaleState.php b/src/State/LocaleState.php index ef198911d..77a6ccd8a 100644 --- a/src/State/LocaleState.php +++ b/src/State/LocaleState.php @@ -10,9 +10,8 @@ final class LocaleState { public function __construct( - private string $locale = 'en' - ) { - } + private string $locale = 'en', + ) {} /** * Set the specified locale code. diff --git a/src/State/StateTrait.php b/src/State/StateTrait.php index bd7c1a30b..2de0b14f0 100644 --- a/src/State/StateTrait.php +++ b/src/State/StateTrait.php @@ -7,6 +7,7 @@ use InvalidArgumentException; use function is_array; +use function sprintf; /** * @internal @@ -70,7 +71,7 @@ public function addToParameter(string $id, mixed ...$value): static $array = $this->parameters[$id] ?? []; if (!is_array($array)) { throw new InvalidArgumentException( - sprintf('The "%s" parameter already exists and is not an array.', $id) + sprintf('The "%s" parameter already exists and is not an array.', $id), ); } diff --git a/src/State/ThemeState.php b/src/State/ThemeState.php index d7d2f189c..c1e3887f9 100644 --- a/src/State/ThemeState.php +++ b/src/State/ThemeState.php @@ -12,9 +12,8 @@ final class ThemeState { public function __construct( - private ?Theme $theme = null - ) { - } + private ?Theme $theme = null, + ) {} /** * Set the specified view theme. diff --git a/src/State/WebViewState.php b/src/State/WebViewState.php index ad746ef66..8ce951cda 100644 --- a/src/State/WebViewState.php +++ b/src/State/WebViewState.php @@ -13,11 +13,14 @@ use Yiisoft\Html\Tag\Style; use Yiisoft\Json\Json; use Yiisoft\View\WebView; +use Yiisoft\Assets\AssetBundle; +use Yiisoft\Assets\AssetManager; use function array_key_exists; use function in_array; use function is_array; use function is_string; +use function sprintf; /** * @internal @@ -232,7 +235,7 @@ public function registerCss( string $css, int $position = WebView::POSITION_HEAD, array $attributes = [], - ?string $key = null + ?string $key = null, ): void { $this->css[$position][$key ?? md5($css)] = $attributes === [] ? $css : Html::style($css, $attributes); } @@ -248,7 +251,7 @@ public function registerCssFromFile( string $path, int $position = WebView::POSITION_HEAD, array $attributes = [], - ?string $key = null + ?string $key = null, ): void { $css = file_get_contents($path); if ($css === false) { @@ -272,11 +275,11 @@ public function registerStyleTag(Style $style, int $position = WebView::POSITION * Registers a CSS file. * * This method should be used for simple registration of CSS files. If you want to use features of - * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use - * {@see \Yiisoft\Assets\AssetBundle}. + * {@see AssetManager} like appending timestamps to the URL and file publishing options, use + * {@see AssetBundle}. * * @param string $url The CSS file to be registered. - * @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} + * @param array $options the HTML attributes for the link tag. Please refer to {@see Html::cssFile()} * for the supported options. * @param string|null $key The key that identifies the CSS script file. If `null`, it will use `$url` as the key. * If two CSS files are registered with the same key, the latter will overwrite the former. @@ -285,7 +288,7 @@ public function registerCssFile( string $url, int $position = WebView::POSITION_HEAD, array $options = [], - ?string $key = null + ?string $key = null, ): void { if (!$this->isValidCssPosition($position)) { throw new InvalidArgumentException('Invalid position of CSS file.'); @@ -357,8 +360,8 @@ public function registerScriptTag(Script $script, int $position = WebView::POSIT * Registers a JS file. * * This method should be used for simple registration of JS files. If you want to use features of - * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use - * {@see \Yiisoft\Assets\AssetBundle}. + * {@see AssetManager} like appending timestamps to the URL and file publishing options, use + * {@see AssetBundle}. * * @param string $url The JS file to be registered. * @param array $options The HTML attributes for the script tag. The following options are specially handled and @@ -369,7 +372,7 @@ public function registerScriptTag(Script $script, int $position = WebView::POSIT * * {@see WebView::POSITION_BEGIN}: at the beginning of the body section * * {@see WebView::POSITION_END}: at the end of the body section. This is the default value. * - * Please refer to {@see \Yiisoft\Html\Html::javaScriptFile()} for other supported options. + * Please refer to {@see Html::javaScriptFile()} for other supported options. * @param string|null $key The key that identifies the JS script file. If null, it will use $url as the key. * If two JS files are registered with the same key at the same position, the latter will overwrite the former. * Note that position option takes precedence, thus files registered with the same key, but different @@ -379,7 +382,7 @@ public function registerJsFile( string $url, int $position = WebView::POSITION_END, array $options = [], - ?string $key = null + ?string $key = null, ): void { if (!$this->isValidJsPosition($position)) { throw new InvalidArgumentException('Invalid position of JS file.'); @@ -437,7 +440,7 @@ public function addJsStrings(array $jsStrings): void foreach ($jsStrings as $key => $value) { $this->registerJsStringByConfig( is_string($key) ? $key : null, - is_array($value) ? $value : [$value, WebView::POSITION_END] + is_array($value) ? $value : [$value, WebView::POSITION_END], ); } } @@ -492,7 +495,7 @@ private function registerCssFileByConfig(?string $key, array $config): void sprintf( 'CSS file should be string. Got %s.', get_debug_type($file), - ) + ), ); } @@ -517,7 +520,7 @@ private function registerCssStringByConfig(?string $key, array $config): void sprintf( 'CSS string should be string or instance of \\' . Style::class . '. Got %s.', get_debug_type($css), - ) + ), ); } @@ -567,7 +570,7 @@ private function registerJsFileByConfig(?string $key, array $config): void sprintf( 'JS file should be string. Got %s.', get_debug_type($file), - ) + ), ); } @@ -592,7 +595,7 @@ private function registerJsStringByConfig(?string $key, array $config): void sprintf( 'JS string should be string or instance of \\' . Script::class . '. Got %s.', get_debug_type($js), - ) + ), ); } @@ -626,7 +629,7 @@ private function registerJsVarByConfig(array $config): void sprintf( 'JS variable name should be string. Got %s.', get_debug_type($key), - ) + ), ); } diff --git a/src/Theme.php b/src/Theme.php index 6ceb63d96..465348586 100644 --- a/src/Theme.php +++ b/src/Theme.php @@ -201,7 +201,7 @@ private function validatePathMap(array $pathMap): void } /** @var mixed $destination */ - foreach ((array)$destinations as $destination) { + foreach ((array) $destinations as $destination) { if (!is_string($destination)) { $this->throwInvalidPathMapException(); } @@ -212,7 +212,7 @@ private function validatePathMap(array $pathMap): void private function throwInvalidPathMapException(): never { throw new InvalidArgumentException( - 'The path map should contain the mapping between view directories and corresponding theme directories.' + 'The path map should contain the mapping between view directories and corresponding theme directories.', ); } } diff --git a/src/View.php b/src/View.php index 9705c1002..d132b3a75 100644 --- a/src/View.php +++ b/src/View.php @@ -98,7 +98,7 @@ protected function createBeforeRenderEvent(string $viewFile, array $parameters): protected function createAfterRenderEvent( string $viewFile, array $parameters, - string $result + string $result, ): AfterRenderEventInterface { return new AfterRender($this, $viewFile, $parameters, $result); } diff --git a/src/ViewContext.php b/src/ViewContext.php index 477add298..a9e8c186a 100644 --- a/src/ViewContext.php +++ b/src/ViewContext.php @@ -7,9 +7,8 @@ final class ViewContext implements ViewContextInterface { public function __construct( - private readonly string $viewPath - ) { - } + private readonly string $viewPath, + ) {} public function getViewPath(): string { diff --git a/src/ViewInterface.php b/src/ViewInterface.php index 7d88d7abe..b8b2e8f4d 100644 --- a/src/ViewInterface.php +++ b/src/ViewInterface.php @@ -24,7 +24,7 @@ interface ViewInterface * * @param string|null $basePath The base path to the view directory. */ - public function withBasePath(string|null $basePath): static; + public function withBasePath(?string $basePath): static; /** * Returns a new instance with the specified renderers. @@ -55,7 +55,7 @@ public function withSourceLocale(string $locale): static; * * @param ViewContextInterface|null $context The context under which the {@see render()} method is being invoked. */ - public function withContext(ViewContextInterface|null $context): static; + public function withContext(?ViewContextInterface $context): static; /** * Returns a new instance with the specified view context path. diff --git a/src/ViewTrait.php b/src/ViewTrait.php index a3d341f35..d4a09d041 100644 --- a/src/ViewTrait.php +++ b/src/ViewTrait.php @@ -13,6 +13,8 @@ use Yiisoft\View\Exception\ViewNotFoundException; use Yiisoft\View\State\LocaleState; use Yiisoft\View\State\ThemeState; +use Yiisoft\View\Event\View\AfterRender; +use Yiisoft\View\Event\View\BeforeRender; use function array_merge; use function array_pop; @@ -24,6 +26,13 @@ use function is_file; use function pathinfo; use function substr; +use function is_object; +use function is_string; +use function sprintf; +use function strlen; + +use const DIRECTORY_SEPARATOR; +use const PATHINFO_EXTENSION; /** * `ViewTrait` could be used as a base implementation of {@see ViewInterface}. @@ -67,7 +76,7 @@ trait ViewTrait * * @param string|null $basePath The base path to the view directory. */ - public function withBasePath(string|null $basePath): static + public function withBasePath(?string $basePath): static { $new = clone $this; $new->basePath = $basePath; @@ -98,8 +107,8 @@ public function withRenderers(array $renderers): static sprintf( 'Extension must be a non-empty string, %s provided for %s.', get_debug_type($extension), - $renderer::class - ) + $renderer::class, + ), ); } @@ -109,8 +118,8 @@ public function withRenderers(array $renderers): static throw new InvalidArgumentException( sprintf( 'Empty extension is not supported. Please add extension for %s.', - $rendererType - ) + $rendererType, + ), ); } @@ -120,15 +129,15 @@ public function withRenderers(array $renderers): static sprintf( 'Renderer %s is not an instance of %s.', $rendererType, - TemplateRendererInterface::class - ) + TemplateRendererInterface::class, + ), ); } } uksort( $renderers, - static fn(string $a, string $b): int => strlen($b) <=> strlen($a) + static fn(string $a, string $b): int => strlen($b) <=> strlen($a), ); $new = clone $this; @@ -166,7 +175,7 @@ public function withFallbackExtension(string $fallbackExtension, string ...$othe * * @param ViewContextInterface|null $context The context under which the {@see render()} method is being invoked. */ - public function withContext(ViewContextInterface|null $context): static + public function withContext(?ViewContextInterface $context): static { $new = clone $this; $new->context = $context; @@ -572,7 +581,7 @@ abstract protected function createBeforeRenderEvent(string $viewFile, array $par abstract protected function createAfterRenderEvent( string $viewFile, array $parameters, - string $result + string $result, ): AfterRenderEventInterface; /** @@ -599,8 +608,8 @@ private function getRenderer(string $viewFile): TemplateRendererInterface /** * This method is invoked right before {@see render()} renders a view file. * - * The default implementations will trigger the {@see \Yiisoft\View\Event\View\BeforeRender} - * or {@see \Yiisoft\View\Event\WebView\BeforeRender} event. If you override this method, + * The default implementations will trigger the {@see BeforeRender} + * or {@see Event\WebView\BeforeRender} event. If you override this method, * make sure you call the parent implementation first. * * @param string $viewFile The view file to be rendered. @@ -623,8 +632,8 @@ private function beforeRender(string $viewFile, array $parameters): bool /** * This method is invoked right after {@see render()} renders a view file. * - * The default implementations will trigger the {@see \Yiisoft\View\Event\View\AfterRender} - * or {@see \Yiisoft\View\Event\WebView\AfterRender} event. If you override this method, + * The default implementations will trigger the {@see AfterRender} + * or {@see Event\WebView\AfterRender} event. If you override this method, * make sure you call the parent implementation first. * * @param string $viewFile The view file being rendered. diff --git a/src/WebView.php b/src/WebView.php index 726f22490..b68786506 100644 --- a/src/WebView.php +++ b/src/WebView.php @@ -24,6 +24,8 @@ use Yiisoft\View\State\LocaleState; use Yiisoft\View\State\ThemeState; use Yiisoft\View\State\WebViewState; +use Yiisoft\Assets\AssetBundle; +use Yiisoft\Assets\AssetManager; use function array_merge; use function implode; @@ -42,10 +44,6 @@ final class WebView implements ViewInterface { use ViewTrait; - private WebViewState $state; - private LocaleState $localeState; - private ThemeState $themeState; - /** * This means the location is in the head section. */ @@ -87,6 +85,10 @@ final class WebView implements ViewInterface */ private const PLACEHOLDER_BODY_END = ''; + private WebViewState $state; + private LocaleState $localeState; + private ThemeState $themeState; + /** * @param string|null $basePath The full path to the base directory of views. * @param EventDispatcherInterface|null $eventDispatcher The event dispatcher instance. @@ -192,7 +194,7 @@ public function endPage(bool $ajaxMode = false): void * Renders a view in response to an AJAX request. * * This method is similar to {@see render()} except that it will surround the view being rendered with the calls of - * {@see beginPage()}, {@see head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the + * {@see beginPage()}, {@see self::head()}, {@see beginBody()}, {@see endBody()} and {@see endPage()}. By doing so, the * method is able to inject into the rendering result with JS/CSS scripts and files that are registered with the * view. * @@ -365,7 +367,7 @@ public function registerCss( string $css, int $position = self::POSITION_HEAD, array $attributes = [], - ?string $key = null + ?string $key = null, ): self { $this->state->registerCss($css, $position, $attributes, $key); return $this; @@ -382,7 +384,7 @@ public function registerCssFromFile( string $path, int $position = self::POSITION_HEAD, array $attributes = [], - ?string $key = null + ?string $key = null, ): self { $this->state->registerCssFromFile($path, $position, $attributes, $key); return $this; @@ -403,11 +405,11 @@ public function registerStyleTag(Style $style, int $position = self::POSITION_HE * Registers a CSS file. * * This method should be used for simple registration of CSS files. If you want to use features of - * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use - * {@see \Yiisoft\Assets\AssetBundle}. + * {@see AssetManager} like appending timestamps to the URL and file publishing options, use + * {@see AssetBundle}. * * @param string $url The CSS file to be registered. - * @param array $options the HTML attributes for the link tag. Please refer to {@see \Yiisoft\Html\Html::cssFile()} + * @param array $options the HTML attributes for the link tag. Please refer to {@see Html::cssFile()} * for the supported options. * @param string|null $key The key that identifies the CSS script file. If null, it will use $url as the key. * If two CSS files are registered with the same key, the latter will overwrite the former. @@ -416,7 +418,7 @@ public function registerCssFile( string $url, int $position = self::POSITION_HEAD, array $options = [], - ?string $key = null + ?string $key = null, ): self { $this->state->registerCssFile($url, $position, $options, $key); return $this; @@ -477,8 +479,8 @@ public function registerScriptTag(Script $script, int $position = self::POSITION * Registers a JS file. * * This method should be used for simple registration of JS files. If you want to use features of - * {@see \Yiisoft\Assets\AssetManager} like appending timestamps to the URL and file publishing options, use - * {@see \Yiisoft\Assets\AssetBundle}. + * {@see AssetManager} like appending timestamps to the URL and file publishing options, use + * {@see AssetBundle}. * * @param string $url The JS file to be registered. * @param array $options The HTML attributes for the script tag. The following options are specially handled and @@ -489,7 +491,7 @@ public function registerScriptTag(Script $script, int $position = self::POSITION * * {@see POSITION_BEGIN}: at the beginning of the body section * * {@see POSITION_END}: at the end of the body section. This is the default value. * - * Please refer to {@see \Yiisoft\Html\Html::javaScriptFile()} for other supported options. + * Please refer to {@see Html::javaScriptFile()} for other supported options. * @param string|null $key The key that identifies the JS script file. If null, it will use $url as the key. * If two JS files are registered with the same key at the same position, the latter will overwrite the former. * Note that position option takes precedence, thus files registered with the same key, but different @@ -499,7 +501,7 @@ public function registerJsFile( string $url, int $position = self::POSITION_END, array $options = [], - ?string $key = null + ?string $key = null, ): self { $this->state->registerJsFile($url, $position, $options, $key); return $this; @@ -568,7 +570,7 @@ protected function createBeforeRenderEvent(string $viewFile, array $parameters): protected function createAfterRenderEvent( string $viewFile, array $parameters, - string $result + string $result, ): AfterRenderEventInterface { return new AfterRender($this, $viewFile, $parameters, $result); } @@ -678,15 +680,15 @@ private function renderBodyEndHtml(bool $ajaxMode): string $lines[] = $this->generateJs($this->state->getJs()[self::POSITION_END]); } if (!empty($this->state->getJs()[self::POSITION_READY])) { - $js = "document.addEventListener('DOMContentLoaded', function(event) {\n" . - $this->generateJsWithoutTag($this->state->getJs()[self::POSITION_READY]) . - "\n});"; + $js = "document.addEventListener('DOMContentLoaded', function(event) {\n" + . $this->generateJsWithoutTag($this->state->getJs()[self::POSITION_READY]) + . "\n});"; $lines[] = Html::script($js)->render(); } if (!empty($this->state->getJs()[self::POSITION_LOAD])) { - $js = "window.addEventListener('load', function(event) {\n" . - $this->generateJsWithoutTag($this->state->getJs()[self::POSITION_LOAD]) . - "\n});"; + $js = "window.addEventListener('load', function(event) {\n" + . $this->generateJsWithoutTag($this->state->getJs()[self::POSITION_LOAD]) + . "\n});"; $lines[] = Html::script($js)->render(); } } diff --git a/tests/Benchmark/BlocksBench.php b/tests/Benchmark/BlocksBench.php index e82a80d66..439fd9bc0 100644 --- a/tests/Benchmark/BlocksBench.php +++ b/tests/Benchmark/BlocksBench.php @@ -30,7 +30,7 @@ public function __construct() $this->setBlock('block-id-1', '...content of block1...'); $this->setBlock('block-id-2', '...content of block2...'); -PHP +PHP, ); } @@ -56,7 +56,7 @@ public function __construct() ... default content for block2 ... -PHP +PHP, ); } diff --git a/tests/Benchmark/MultiRendererBench.php b/tests/Benchmark/MultiRendererBench.php index 72cf88867..4509986dc 100644 --- a/tests/Benchmark/MultiRendererBench.php +++ b/tests/Benchmark/MultiRendererBench.php @@ -31,7 +31,7 @@ public function __construct() /** @var \Yiisoft\View\View $this */ echo 'PHP view'; - PHP_WRAP + PHP_WRAP, ); } @@ -47,13 +47,13 @@ public function __construct() $this->view = (new View($basePath, new SimpleEventDispatcher())) ->withRenderers([ - 'blade' => new class () implements TemplateRendererInterface { + 'blade' => new class implements TemplateRendererInterface { public function render(ViewInterface $view, string $template, array $parameters): string { return file_get_contents($template); } }, - 'tpl' => new class () implements TemplateRendererInterface { + 'tpl' => new class implements TemplateRendererInterface { public function render(ViewInterface $view, string $template, array $parameters): string { return file_get_contents($template); diff --git a/tests/Benchmark/NestedViewsBench.php b/tests/Benchmark/NestedViewsBench.php index 9f6c473b5..e9b5de138 100644 --- a/tests/Benchmark/NestedViewsBench.php +++ b/tests/Benchmark/NestedViewsBench.php @@ -32,7 +32,7 @@ public function __construct() /** @var \Yiisoft\View\View $this */ echo $this->render('./sub/sub'); -PHP +PHP, ); } diff --git a/tests/Benchmark/ViewThemeBench.php b/tests/Benchmark/ViewThemeBench.php index 73831964e..7a0cdc299 100644 --- a/tests/Benchmark/ViewThemeBench.php +++ b/tests/Benchmark/ViewThemeBench.php @@ -8,6 +8,8 @@ use Yiisoft\View\Theme; use Yiisoft\View\View; +use function dirname; + final class ViewThemeBench { private readonly View $view; diff --git a/tests/CachedContentTest.php b/tests/CachedContentTest.php index eff67c144..f5b594566 100644 --- a/tests/CachedContentTest.php +++ b/tests/CachedContentTest.php @@ -43,7 +43,7 @@ public function testCache(?int $ttl, ?Dependency $dependency): void for ($counter = 0; $counter < 42; $counter++) { $dynamicContent = new DynamicContent( 'dynamic-test', - static fn ($params) => (string) $params['counter'], + static fn($params) => (string) $params['counter'], ['counter' => $counter], ); @@ -64,7 +64,7 @@ public function testCacheWithExpiredTtl(): void for ($counter = 0; $counter < 42; $counter++) { $dynamicContent = new DynamicContent( 'dynamic-test', - static fn ($params) => (string) $params['counter'], + static fn($params) => (string) $params['counter'], ['counter' => $counter], ); @@ -82,7 +82,7 @@ public function testCacheWithVariants(): void for ($counter = 0; $counter < 42; $counter++) { $dynamicContent = new DynamicContent( 'dynamic-test', - static fn ($params) => (string) $params['counter'], + static fn($params) => (string) $params['counter'], ['counter' => $counter], ); @@ -112,7 +112,7 @@ public function testTwoCache(): void public function testCacheWithSimilarKey(): void { - $this->cache->getOrSet(CacheKeyNormalizer::normalize(['test']), static fn () => 'hello'); + $this->cache->getOrSet(CacheKeyNormalizer::normalize(['test']), static fn() => 'hello'); $cacheContent = new CachedContent('test', $this->cache); $cacheContent->cache('content'); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 3d9f2cbd5..66c833d1a 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -83,11 +83,10 @@ private function createContainer(?string $postfix = null, ?array $params = null) return new Container( ContainerConfig::create()->withDefinitions( $this->getDiConfig($postfix, $params) - + - [ + + [ EventDispatcherInterface::class => new SimpleEventDispatcher(), - ] - ) + ], + ), ); } diff --git a/tests/TestSupport/StringableObject.php b/tests/TestSupport/StringableObject.php index f781cdac5..f8021bb24 100644 --- a/tests/TestSupport/StringableObject.php +++ b/tests/TestSupport/StringableObject.php @@ -10,8 +10,7 @@ final class StringableObject implements Stringable { public function __construct( private readonly string $value = '', - ) { - } + ) {} public function __toString(): string { diff --git a/tests/TestSupport/TestTrait.php b/tests/TestSupport/TestTrait.php index 46cd72282..a3a3a5de1 100644 --- a/tests/TestSupport/TestTrait.php +++ b/tests/TestSupport/TestTrait.php @@ -22,7 +22,7 @@ protected function assertSameIgnoringSlash(string $expected, string $actual): vo protected function assertEqualStringsIgnoringLineEndings( string $expected, string $actual, - string $message = '' + string $message = '', ): void { $expected = self::normalizeLineEndings($expected); $actual = self::normalizeLineEndings($actual); diff --git a/tests/ThemeTest.php b/tests/ThemeTest.php index 511752399..e1238a54c 100644 --- a/tests/ThemeTest.php +++ b/tests/ThemeTest.php @@ -174,7 +174,7 @@ public function testInvalidPathMapKey(): void public function testInvalidPathMapValue(): void { $this->expectExceptionMessage( - 'The path map should contain the mapping between view directories and corresponding theme directories.' + 'The path map should contain the mapping between view directories and corresponding theme directories.', ); $this->expectException(InvalidArgumentException::class); diff --git a/tests/View/RelativePath/RelativePathTest.php b/tests/View/RelativePath/RelativePathTest.php index 4c6e1cc4f..afac97fe9 100644 --- a/tests/View/RelativePath/RelativePathTest.php +++ b/tests/View/RelativePath/RelativePathTest.php @@ -31,7 +31,7 @@ public function testRelativeWithoutCurrentView(string $template): void $this->expectException(LogicException::class); $this->expectExceptionMessage( - 'Unable to resolve file for view "' . $template . '": no currently rendered view.' + 'Unable to resolve file for view "' . $template . '": no currently rendered view.', ); $view->render($template); } diff --git a/tests/View/RelativePath/views-parent/template/nested/template.php b/tests/View/RelativePath/views-parent/template/nested/template.php index 9943a5746..887fd9572 100644 --- a/tests/View/RelativePath/views-parent/template/nested/template.php +++ b/tests/View/RelativePath/views-parent/template/nested/template.php @@ -1,9 +1,10 @@ Exception -PHP +PHP, ); $normalViewFile = $this->tempDirectory . DIRECTORY_SEPARATOR . 'no-exception.php'; file_put_contents( $normalViewFile, <<<'PHP'