Skip to content

Commit

Permalink
Add rector, raise minimum PHP version to ^8.0, change return type o…
Browse files Browse the repository at this point in the history
…f immutable methods in `ViewInterface` from `self` to `static`
  • Loading branch information
xepozz committed Nov 13, 2022
1 parent b87d31d commit f14de1e
Show file tree
Hide file tree
Showing 28 changed files with 190 additions and 351 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
@@ -0,0 +1,21 @@
on:
pull_request:
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
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
5 changes: 3 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,9 @@
# Yii View Change Log

## 6.0.1 under development
## 7.0.0 under development

- no changes in this release.
- Enh #211: Raise minimum PHP version to `^8.0` (@xepozz, @vjik)
- Chg #211: Change return type of immutable methods in `ViewInterface` from `self` to `static` (@vjik)

## 6.0.0 July 21, 2022

Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -21,14 +21,14 @@ to be usable separately.

## Requirements

- PHP 7.4 or higher.
- PHP 8.0 or higher.

## Installation

The package could be installed via composer:

```shell
composer require yiisoft/view --prefer-dist
composer require yiisoft/view
```

## General usage
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -27,7 +27,7 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"psr/event-dispatcher": "1.0.0",
"psr/event-dispatcher-implementation": "1.0.0",
"yiisoft/arrays": "^2.0",
Expand All @@ -38,6 +38,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.18",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.24",
Expand Down
29 changes: 29 additions & 0 deletions rector.php
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php70\Rector\FuncCall\NonVariableToVariableOnFunctionCallRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
NonVariableToVariableOnFunctionCallRector::class => [__DIR__ . '/src/PhpTemplateRenderer.php'],
RemoveExtraParametersRector::class => [__DIR__ . '/src/PhpTemplateRenderer.php'],
]);
};
21 changes: 9 additions & 12 deletions src/Cache/CachedContent.php
Expand Up @@ -11,10 +11,7 @@
use Yiisoft\Cache\Dependency\Dependency;

use function array_merge;
use function get_class;
use function gettype;
use function is_string;
use function is_object;
use function sprintf;
use function strtr;

Expand All @@ -23,8 +20,6 @@
*/
final class CachedContent
{
private string $id;
private CacheInterface $cache;
private CacheKeyNormalizer $cacheKeyNormalizer;

/**
Expand All @@ -43,10 +38,12 @@ final class CachedContent
* @param DynamicContent[] $dynamicContents The dynamic content instances.
* @param string[] $variations List of string factors that would cause the variation of the content being cached.
*/
public function __construct(string $id, CacheInterface $cache, array $dynamicContents = [], array $variations = [])
{
$this->id = $id;
$this->cache = $cache;
public function __construct(
private string $id,
private CacheInterface $cache,
array $dynamicContents = [],
array $variations = []
) {
$this->cacheKeyNormalizer = new CacheKeyNormalizer();
$this->setDynamicContents($dynamicContents);
$this->setVariations($variations);
Expand Down Expand Up @@ -98,7 +95,7 @@ public function get(): ?string
*/
private function cacheKey(): string
{
return $this->cacheKeyNormalizer->normalize(array_merge([__CLASS__, $this->id], $this->variations));
return $this->cacheKeyNormalizer->normalize(array_merge([self::class, $this->id], $this->variations));
}

/**
Expand Down Expand Up @@ -134,7 +131,7 @@ private function setDynamicContents(array $dynamicContents): void
if (!($dynamicContent instanceof DynamicContent)) {
throw new InvalidArgumentException(sprintf(
'Invalid dynamic content "%s" specified. It must be a "%s" instance.',
is_object($dynamicContent) ? get_class($dynamicContent) : gettype($dynamicContent),
get_debug_type($dynamicContent),
DynamicContent::class,
));
}
Expand All @@ -154,7 +151,7 @@ private function setVariations(array $variations): void
if (!is_string($variation)) {
throw new InvalidArgumentException(sprintf(
'Invalid variation "%s" specified. It must be a string type.',
is_object($variation) ? get_class($variation) : gettype($variation),
get_debug_type($variation),
));
}

Expand Down
12 changes: 5 additions & 7 deletions src/Cache/DynamicContent.php
Expand Up @@ -9,9 +9,6 @@
*/
final class DynamicContent
{
private string $id;
private array $parameters;

/**
* @var callable
*/
Expand All @@ -22,11 +19,12 @@ final class DynamicContent
* @param callable $contentGenerator PHP callable with the signature: `function (array $parameters = []): string;`.
* @param array $parameters The parameters (name-value pairs) that will be passed in the $contentGenerator context.
*/
public function __construct(string $id, callable $contentGenerator, array $parameters = [])
{
$this->id = $id;
public function __construct(
private string $id,
callable $contentGenerator,
private array $parameters = []
) {
$this->contentGenerator = $contentGenerator;
$this->parameters = $parameters;
}

/**
Expand Down
27 changes: 9 additions & 18 deletions src/Event/View/AfterRender.php
Expand Up @@ -12,26 +12,17 @@
*/
final class AfterRender implements AfterRenderEventInterface
{
private View $view;

/**
* @var string The view file being rendered.
*/
private string $file;

/**
* @var array The parameters array passed to the {@see View::render()} or {@see View::renderFile()} method.
* @param string $file The view file being rendered.
* @param array $parameters The parameters array passed to the {@see View::render()} or {@see View::renderFile()}
* method.
*/
private array $parameters;

private string $result;

public function __construct(View $view, string $file, array $parameters, string $result)
{
$this->view = $view;
$this->file = $file;
$this->parameters = $parameters;
$this->result = $result;
public function __construct(
private View $view,
private string $file,
private array $parameters,
private string $result
) {
}

public function getView(): View
Expand Down
25 changes: 9 additions & 16 deletions src/Event/View/BeforeRender.php
Expand Up @@ -12,25 +12,18 @@
*/
final class BeforeRender implements StoppableEventInterface
{
private View $view;

/**
* @var string The view file being rendered.
*/
private string $file;
private bool $stopPropagation = false;

/**
* @var array The parameters array passed to the {@see View::render()} or {@see View::renderFile()} method.
* @param string $file The view file being rendered.
* @param array $parameters The parameters array passed to the {@see View::render()} or {@see View::renderFile()}
* method.
*/
private array $parameters;

private bool $stopPropagation = false;

public function __construct(View $view, string $file, array $parameters)
{
$this->view = $view;
$this->file = $file;
$this->parameters = $parameters;
public function __construct(
private View $view,
private string $file,
private array $parameters
) {
}

public function stopPropagation(): void
Expand Down
8 changes: 3 additions & 5 deletions src/Event/View/ViewEvent.php
Expand Up @@ -11,11 +11,9 @@
*/
abstract class ViewEvent
{
private View $view;

final public function __construct(View $view)
{
$this->view = $view;
final public function __construct(
private View $view
) {
}

final public function getView(): View
Expand Down
27 changes: 9 additions & 18 deletions src/Event/WebView/AfterRender.php
Expand Up @@ -12,26 +12,17 @@
*/
final class AfterRender implements AfterRenderEventInterface
{
private WebView $view;

/**
* @var string The view file being rendered.
*/
private string $file;

/**
* @var array The parameters array passed to the {@see WebView::render()} or {@see WebView::renderFile()} method.
* @param string $file The view file being rendered.
* @param array $parameters The parameters array passed to the {@see WebView::render()}
* or {@see WebView::renderFile()} method.
*/
private array $parameters;

private string $result;

public function __construct(WebView $view, string $file, array $parameters, string $result)
{
$this->view = $view;
$this->file = $file;
$this->parameters = $parameters;
$this->result = $result;
public function __construct(
private WebView $view,
private string $file,
private array $parameters,
private string $result
) {
}

public function getView(): WebView
Expand Down
25 changes: 9 additions & 16 deletions src/Event/WebView/BeforeRender.php
Expand Up @@ -12,25 +12,18 @@
*/
final class BeforeRender implements StoppableEventInterface
{
private WebView $view;

/**
* @var string The view file being rendered.
*/
private string $file;
private bool $stopPropagation = false;

/**
* @var array The parameters array passed to the {@see WebView::render()} or {@see WebView::renderFile()} method.
* @param string $file The view file being rendered.
* @param array $parameters The parameters array passed to the {@see WebView::render()}
* or {@see WebView::renderFile()} method.
*/
private array $parameters;

private bool $stopPropagation = false;

public function __construct(WebView $view, string $file, array $parameters)
{
$this->view = $view;
$this->file = $file;
$this->parameters = $parameters;
public function __construct(
private WebView $view,
private string $file,
private array $parameters
) {
}

public function stopPropagation(): void
Expand Down
8 changes: 3 additions & 5 deletions src/Event/WebView/WebViewEvent.php
Expand Up @@ -11,11 +11,9 @@
*/
abstract class WebViewEvent
{
private WebView $view;

final public function __construct(WebView $view)
{
$this->view = $view;
final public function __construct(
private WebView $view
) {
}

final public function getView(): WebView
Expand Down

0 comments on commit f14de1e

Please sign in to comment.