Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rector [batch] #506

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

env:
extensions: fileinfo, pdo, pdo_sqlite, intl
extensions: fileinfo, pdo, pdo_sqlite, intl-70.1
key: cache-v1
YII_C3: true

Expand Down
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: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@
"codeception/module-cli": "^2.0",
"codeception/module-phpbrowser": "^3.0",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"roave/security-advisories": "dev-master",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18",
"yiisoft/translator-extractor": "^1.0",
"yiisoft/yii-debug-viewer": "^3.0@dev",
"yiisoft/yii-gii": "^3.0@dev",
"yiisoft/yii-testing": "dev-master"
},
"autoload": {
Expand Down
20 changes: 20 additions & 0 deletions config/params-web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Yiisoft\Assets\AssetManager;
use Yiisoft\Definitions\Reference;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Translator\TranslatorInterface;

return [
'yiisoft/view' => [
'parameters' => [
'assetManager' => Reference::to(AssetManager::class),
'urlGenerator' => Reference::to(UrlGeneratorInterface::class),
'currentRoute' => Reference::to(CurrentRoute::class),
'translator' => Reference::to(TranslatorInterface::class),
],
],
];
21 changes: 11 additions & 10 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
use App\ViewInjection\LayoutViewInjection;
use App\ViewInjection\LinkTagsViewInjection;
use App\ViewInjection\MetaTagsViewInjection;
use Yiisoft\Assets\AssetManager;
use Cycle\Database\Config\SQLite\FileConnectionConfig;
use Cycle\Database\Config\SQLiteDriverConfig;
use Yiisoft\Cookies\CookieMiddleware;
use Yiisoft\Definitions\Reference;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Form\Field\SubmitButton;
use Yiisoft\Router\Middleware\Router;
use Yiisoft\Router\UrlGeneratorInterface;
use Yiisoft\Session\SessionMiddleware;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\User\Login\Cookie\CookieLoginMiddleware;
use Yiisoft\Yii\Console\Application;
use Yiisoft\Yii\Console\Command\Serve;
use Yiisoft\Yii\Cycle\Schema\Conveyor\AttributedSchemaConveyor;
use Yiisoft\Yii\Middleware\Locale;
use Yiisoft\Yii\Cycle\Schema\Provider\FromConveyorSchemaProvider;
use Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider;
use Yiisoft\Yii\Sentry\SentryMiddleware;
use Yiisoft\Yii\View\CsrfViewInjection;

Expand Down Expand Up @@ -74,7 +75,7 @@
'errorClass' => 'fw-bold fst-italic',
'hintClass' => 'form-text',
'fieldConfigs' => [
\Yiisoft\Form\Field\SubmitButton::class => [
SubmitButton::class => [
'buttonClass()' => ['btn btn-primary btn-lg mt-3'],
'containerClass()' => ['d-grid gap-2 form-floating'],
],
Expand Down Expand Up @@ -156,8 +157,8 @@
'default' => ['connection' => 'sqlite'],
],
'connections' => [
'sqlite' => new \Cycle\Database\Config\SQLiteDriverConfig(
connection: new \Cycle\Database\Config\SQLite\FileConnectionConfig(
'sqlite' => new SQLiteDriverConfig(
connection: new FileConnectionConfig(
database: 'runtime/database.db'
)
),
Expand Down Expand Up @@ -195,12 +196,12 @@
// \Yiisoft\Yii\Cycle\Schema\Provider\SimpleCacheSchemaProvider::class => ['key' => 'cycle-orm-cache-key'],

// Store generated Schema in the file
\Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::class => [
'mode' => \Yiisoft\Yii\Cycle\Schema\Provider\PhpFileSchemaProvider::MODE_WRITE_ONLY,
PhpFileSchemaProvider::class => [
'mode' => PhpFileSchemaProvider::MODE_WRITE_ONLY,
'file' => 'runtime/schema.php',
],

\Yiisoft\Yii\Cycle\Schema\Provider\FromConveyorSchemaProvider::class => [
FromConveyorSchemaProvider::class => [
'generators' => [
Cycle\Schema\Generator\SyncTables::class, // sync table changes to database
],
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,
]);
};
5 changes: 0 additions & 5 deletions src/Auth/IdentityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public function __construct(private EntityWriter $entityWriter, Select $select)
parent::__construct($select);
}

/**
* @param string $id
*
* @return Identity|null
*/
public function findIdentity(string $id): ?Identity
{
return $this->findOne(['user_id' => $id]);
Expand Down
8 changes: 1 addition & 7 deletions src/Blog/Archive/ArchiveRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ public function getFullArchive(): DataReaderInterface

/**
* @param string $attr Can be 'day', 'month' or 'year'
*
* @return FragmentInterface
*/
private function extractFromDateColumn(string $attr): FragmentInterface
{
Expand Down Expand Up @@ -121,12 +119,8 @@ private function getDriver(): DriverInterface

/**
* @psalm-suppress UndefinedDocblockClass
*
* @param Select|SelectQuery $query
*
* @return EntityReader
*/
private function prepareDataReader($query): EntityReader
private function prepareDataReader(Select|SelectQuery $query): EntityReader
{
return (new EntityReader($query))->withSort(Sort::only(['published_at'])->withOrder(['published_at' => 'desc']));
}
Expand Down
5 changes: 1 addition & 4 deletions src/Blog/Comment/Scope/PublicScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
*/
final class PublicScope implements ConstrainInterface
{
private ?array $publicOrCondition;

public function __construct(?array $publicOrCondition = null)
public function __construct(private ?array $publicOrCondition = null)
{
$this->publicOrCondition = $publicOrCondition;
}

public function apply(QueryBuilder $query): void
Expand Down
6 changes: 1 addition & 5 deletions src/Blog/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ class Comment
#[Column(type: 'bool', default: 'false', typecast: 'bool')]
private bool $public = false;

#[Column(type: 'text')]
private string $content;

#[Column(type: 'datetime')]
private DateTimeImmutable $created_at;

Expand All @@ -51,9 +48,8 @@ class Comment
private ?Post $post = null;
private ?int $post_id = null;

public function __construct(string $content)
public function __construct(#[Column(type: 'text')] private string $content)
{
$this->content = $content;
$this->created_at = new DateTimeImmutable();
$this->updated_at = new DateTimeImmutable();
}
Expand Down
10 changes: 1 addition & 9 deletions src/Blog/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ class Post
#[Column(type: 'string(128)')]
private string $slug;

#[Column(type: 'string(191)', default: '')]
private string $title = '';

#[Column(type: 'bool', default: 'false', typecast: 'bool')]
private bool $public = false;

#[Column(type: 'text')]
private string $content = '';

#[Column(type: 'datetime')]
private DateTimeImmutable $created_at;

Expand Down Expand Up @@ -70,10 +64,8 @@ class Post
#[HasMany(target: Comment::class)]
private ArrayCollection $comments;

public function __construct(string $title = '', string $content = '')
public function __construct(#[Column(type: 'string(191)', default: '')] private string $title = '', #[Column(type: 'text')] private string $content = '')
{
$this->title = $title;
$this->content = $content;
$this->created_at = new DateTimeImmutable();
$this->updated_at = new DateTimeImmutable();
$this->tags = new PivotedCollection();
Expand Down
6 changes: 1 addition & 5 deletions src/Blog/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class Tag
#[Column(type: 'primary')]
private ?int $id = null;

#[Column(type: 'string(191)')]
private string $label;

#[Column(type: 'datetime')]
private DateTimeImmutable $created_at;

Expand All @@ -32,9 +29,8 @@ class Tag
#[ManyToMany(target: Post::class, though: PostTag::class, fkAction: 'CASCADE', indexCreate: false)]
private PivotedCollection $posts;

public function __construct(string $label)
public function __construct(#[Column(type: 'string(191)')] private string $label)
{
$this->label = $label;
$this->created_at = new DateTimeImmutable();
$this->posts = new PivotedCollection();
}
Expand Down
2 changes: 0 additions & 2 deletions src/Blog/Tag/TagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function findByLabel(string $label): ?Tag
}

/**
* @param int $limit
*
* @return DataReaderInterface Collection of Array('label' => 'Tag Label', 'count' => '8')
*/
public function getTagMentions(int $limit = 0): DataReaderInterface
Expand Down
16 changes: 8 additions & 8 deletions src/Command/Fixture/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,29 @@ private function addPosts(int $count): void
for ($i = 0; $i < $count; ++$i) {
/** @var User $postUser */
$postUser = $this->users[array_rand($this->users)];
$post = new Post($this->faker->text(64), $this->faker->realText(rand(1000, 4000)));
$post = new Post($this->faker->text(64), $this->faker->realText(random_int(1000, 4000)));
$postUser->addPost($post);
$public = rand(0, 2) > 0;
$public = random_int(0, 2) > 0;
$post->setPublic($public);
if ($public) {
$post->setPublishedAt(new DateTimeImmutable(date('r', rand(time(), strtotime('-2 years')))));
$post->setPublishedAt(new DateTimeImmutable(date('r', random_int(time(), strtotime('-2 years')))));
}
// link tags
$postTags = (array)array_rand($this->tags, rand(1, count($this->tags)));
$postTags = (array)array_rand($this->tags, random_int(1, count($this->tags)));
foreach ($postTags as $tagId) {
$tag = $this->tags[$tagId];
$post->addTag($tag);
// todo: uncomment when issue is resolved https://github.com/cycle/orm/issues/70
// $tag->addPost($post);
}
// add comments
$commentsCount = rand(0, $count);
$commentsCount = random_int(0, $count);
for ($j = 0; $j <= $commentsCount; ++$j) {
$comment = new Comment($this->faker->realText(rand(100, 500)));
$commentPublic = rand(0, 3) > 0;
$comment = new Comment($this->faker->realText(random_int(100, 500)));
$commentPublic = random_int(0, 3) > 0;
$comment->setPublic($commentPublic);
if ($commentPublic) {
$comment->setPublishedAt(new DateTimeImmutable(date('r', rand(time(), strtotime('-1 years')))));
$comment->setPublishedAt(new DateTimeImmutable(date('r', random_int(time(), strtotime('-1 years')))));
}
$commentUser = $this->users[array_rand($this->users)];
$commentUser->addComment($comment);
Expand Down
4 changes: 1 addition & 3 deletions src/Command/Router/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$routes = $this->routeCollection->getRoutes();
uasort(
$routes,
static function (Route $a, Route $b) {
return ($a->getData('host') <=> $b->getData('host')) ?: ($a->getData('name') <=> $b->getData('name'));
}
static fn (Route $a, Route $b) => ($a->getData('host') <=> $b->getData('host')) ?: ($a->getData('name') <=> $b->getData('name'))
);
$table->setHeaders(['Host', 'Methods', 'Name', 'Pattern', 'Defaults']);
foreach ($routes as $route) {
Expand Down
5 changes: 1 addition & 4 deletions src/Controller/Actions/ApiInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
*/
final class ApiInfo implements MiddlewareInterface
{
private DataResponseFactoryInterface $responseFactory;

public function __construct(DataResponseFactoryInterface $responseFactory)
public function __construct(private DataResponseFactoryInterface $responseFactory)
{
$this->responseFactory = $responseFactory;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Controller/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

final class SiteController
{
private ViewRenderer $viewRenderer;

public function __construct(ViewRenderer $viewRenderer)
public function __construct(private ViewRenderer $viewRenderer)
{
$this->viewRenderer = $viewRenderer->withController($this);
}
Expand Down
6 changes: 1 addition & 5 deletions src/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class User
#[Column(type: 'primary')]
private ?int $id = null;

#[Column(type: 'string(48)')]
private string $login;

#[Column(type: 'string')]
private string $passwordHash;

Expand All @@ -53,9 +50,8 @@ class User
#[HasMany(target: \App\Blog\Entity\Comment::class)]
private ArrayCollection $comments;

public function __construct(string $login, string $password)
public function __construct(#[Column(type: 'string(48)')] private string $login, string $password)
{
$this->login = $login;
$this->created_at = new DateTimeImmutable();
$this->updated_at = new DateTimeImmutable();
$this->setPassword($password);
Expand Down
5 changes: 0 additions & 5 deletions src/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public function findAll(array $scope = [], array $orderBy = []): DataReaderInter
->orderBy($orderBy));
}

/**
* @param string $id
*
* @return User|null
*/
public function findById(string $id): ?User
{
return $this->findByPK($id);
Expand Down
2 changes: 0 additions & 2 deletions tests/Cli/ConsoleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public function testCommandListCommand(CliTester $I): void
* Clear all data created with testCommandFixtureAdd().
* Clearing database prevents from getting errors during multiple continuous testing with other test,
* what are based on empty database (eg, BlogPageCest)
*
* @param \App\Tests\CliTester $I
*/
public function testCommandCycleSchemaClear(CliTester $I): void
{
Expand Down