Skip to content

Commit

Permalink
Add rector (#62)
Browse files Browse the repository at this point in the history
* Add rector files yiisoft/yii-dev-tool#232

* Add rector/rector dependecy

* [rector] Apply fixes

* Apply fixes from StyleCI

* Use predefined rector action

* Bump PHP version to 8.0

* Add import

Co-authored-by: Dmitrii Derepko <[email protected]>
Co-authored-by: rector-bot <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
4 people authored Oct 12, 2022
1 parent 61e6ec2 commit 64e92e9
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ WEB-specific functionality and compatibility with [PSR-7](https://www.php-fig.or

## Requirements

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

## Installation

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"source": "https://github.com/yiisoft/yii-view"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"yiisoft/aliases": "^2.0",
"yiisoft/csrf": "^1.2",
"yiisoft/data-response": "^1.0",
Expand All @@ -30,6 +30,7 @@
"httpsoft/http-message": "^1.0",
"nyholm/psr7": "^1.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18",
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
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,
]);
};
2 changes: 0 additions & 2 deletions src/CommonParametersInjectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface CommonParametersInjectionInterface
* ]
* ```
*
* @return array
*
* @psalm-return array<string, mixed>
*/
public function getCommonParameters(): array;
Expand Down
12 changes: 3 additions & 9 deletions src/Csrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@

namespace Yiisoft\Yii\View;

use Stringable;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Input;

final class Csrf
final class Csrf implements Stringable
{
private string $token;
private string $parameterName;
private string $headerName;

public function __construct(string $token, string $parameterName, string $headerName)
public function __construct(private string $token, private string $parameterName, private string $headerName)
{
$this->token = $token;
$this->parameterName = $parameterName;
$this->headerName = $headerName;
}

public function getToken(): string
Expand Down
11 changes: 1 addition & 10 deletions src/CsrfViewInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@ final class CsrfViewInjection implements CommonParametersInjectionInterface, Met
private string $metaAttributeName = self::DEFAULT_META_ATTRIBUTE_NAME;
private string $parameterName = self::DEFAULT_PARAMETER_NAME;

private CsrfTokenInterface $token;
private CsrfMiddleware $middleware;

public function __construct(CsrfTokenInterface $token, CsrfMiddleware $middleware)
public function __construct(private CsrfTokenInterface $token, private CsrfMiddleware $middleware)
{
$this->token = $token;
$this->middleware = $middleware;
}

/**
* Returns a new instance with the specified parameter name.
*
* @param string $parameterName The parameter name.
*
* @return self
*/
public function withParameterName(string $parameterName): self
{
Expand All @@ -47,8 +40,6 @@ public function withParameterName(string $parameterName): self
* Returns a new instance with the specified meta attribute name.
*
* @param string $metaAttributeName The meta attribute name.
*
* @return self
*/
public function withMetaAttributeName(string $metaAttributeName): self
{
Expand Down
12 changes: 1 addition & 11 deletions src/Exception/InvalidLinkTagException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@
*/
final class InvalidLinkTagException extends RuntimeException implements FriendlyExceptionInterface
{
/**
* @var mixed
*/
private $tag;

/**
* @param mixed $tag
*/
public function __construct(string $message, $tag)
public function __construct(string $message, private mixed $tag)
{
$this->tag = $tag;

parent::__construct($message);
}

Expand Down
12 changes: 1 addition & 11 deletions src/Exception/InvalidMetaTagException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@
*/
final class InvalidMetaTagException extends RuntimeException implements FriendlyExceptionInterface
{
/**
* @var mixed
*/
private $tag;

/**
* @param mixed $tag
*/
public function __construct(string $message, $tag)
public function __construct(string $message, private mixed $tag)
{
$this->tag = $tag;

parent::__construct($message);
}

Expand Down
2 changes: 0 additions & 2 deletions src/LayoutParametersInjectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ interface LayoutParametersInjectionInterface
* ]
* ```
*
* @return array
*
* @psalm-return array<string, mixed>
*/
public function getLayoutParameters(): array;
Expand Down
2 changes: 0 additions & 2 deletions src/LinkTagsInjectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ interface LinkTagsInjectionInterface
* ]
* ```
*
* @return array
*
* @psalm-return LinkTagsConfig
*/
public function getLinkTags(): array;
Expand Down
48 changes: 8 additions & 40 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

use function array_key_exists;
use function array_merge;
use function get_class;
use function gettype;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function pathinfo;
use function preg_match;
Expand All @@ -45,20 +42,10 @@
*/
final class ViewRenderer implements ViewContextInterface
{
private DataResponseFactoryInterface $responseFactory;
private Aliases $aliases;
private WebView $view;

private string $viewPath;
private ?string $layout;
private ?string $name = null;
private ?string $locale = null;

/**
* @var object[]
*/
private array $injections;

/**
* @param DataResponseFactoryInterface $responseFactory The data response factory instance.
* @param Aliases $aliases The aliases instance.
Expand All @@ -69,19 +56,14 @@ final class ViewRenderer implements ViewContextInterface
* @param object[] $injections The injection instances.
*/
public function __construct(
DataResponseFactoryInterface $responseFactory,
Aliases $aliases,
WebView $view,
private DataResponseFactoryInterface $responseFactory,
private Aliases $aliases,
private WebView $view,
string $viewPath,
?string $layout = null,
array $injections = []
private ?string $layout = null,
private array $injections = []
) {
$this->responseFactory = $responseFactory;
$this->aliases = $aliases;
$this->view = $view;
$this->viewPath = rtrim($viewPath, '/');
$this->layout = $layout;
$this->injections = $injections;
}

/**
Expand Down Expand Up @@ -208,8 +190,6 @@ public function renderPartialAsString(string $view, array $parameters = []): str
* Extracts the controller name and returns a new instance with the controller name.
*
* @param object $controller The controller instance.
*
* @return self
*/
public function withController(object $controller): self
{
Expand All @@ -222,8 +202,6 @@ public function withController(object $controller): self
* Returns a new instance with the specified controller name.
*
* @param string $name The controller name.
*
* @return self
*/
public function withControllerName(string $name): self
{
Expand All @@ -236,8 +214,6 @@ public function withControllerName(string $name): self
* Returns a new instance with the specified view path.
*
* @param string $viewPath The full path to the directory of views or its alias.
*
* @return self
*/
public function withViewPath(string $viewPath): self
{
Expand All @@ -251,8 +227,6 @@ public function withViewPath(string $viewPath): self
*
* @param string|null $layout The layout name (e.g. "layout/main") to be applied to views.
* If null, the layout will not be applied.
*
* @return self
*/
public function withLayout(?string $layout): self
{
Expand All @@ -265,8 +239,6 @@ public function withLayout(?string $layout): self
* Return a new instance with the appended specified injections.
*
* @param object ...$injections The injection instances.
*
* @return self
*/
public function withAddedInjections(object ...$injections): self
{
Expand All @@ -279,8 +251,6 @@ public function withAddedInjections(object ...$injections): self
* Returns a new instance with the specified injections.
*
* @param object ...$injections The injection instances.
*
* @return self
*/
public function withInjections(object ...$injections): self
{
Expand All @@ -293,8 +263,6 @@ public function withInjections(object ...$injections): self
* Returns a new instance with specified locale code.
*
* @param string $locale The locale code.
*
* @return self
*/
public function withLocale(string $locale): self
{
Expand Down Expand Up @@ -556,7 +524,7 @@ private function extractControllerName(object $controller): string
/** @var string[] $cache */
static $cache = [];

$class = get_class($controller);
$class = $controller::class;
if (array_key_exists($class, $cache)) {
return $cache[$class];
}
Expand All @@ -578,8 +546,8 @@ private function extractControllerName(object $controller): string
*
* @return string The value type.
*/
private function getType($value): string
private function getType(mixed $value): string
{
return is_object($value) ? get_class($value) : gettype($value);
return get_debug_type($value);
}
}

0 comments on commit 64e92e9

Please sign in to comment.