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

Welcome to RectorPHP #307

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.0|^11.0",
"larastan/larastan": "^2.0",
"pestphp/pest-plugin-laravel": "^2.3"
"pestphp/pest-plugin-laravel": "^2.3",
"rector/rector": "^1.2",
"driftingly/rector-laravel": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 20 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/config',
__DIR__ . '/routes',
__DIR__ . '/src',
__DIR__ . '/tests',
])
// uncomment to reach your current PHP version
//->withPhpSets(php80: true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in assuming that we'd set the current PHP version as 8.1 here since that's the minimum version required for the package? 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tried it, but it raised some other issues we need to address. So, to keep this specific PR small, I started with two simple rulesets. One of the great things of Rector is that you can introduce it in a project with just a few rules and then add more rules later, with a kind of "progressive" approach

->withPreparedSets(
deadCode: true,
codeQuality: true
)
->withTypeCoverageLevel(0);
4 changes: 2 additions & 2 deletions src/Classes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public function make(): ShortURL

$shortURL = new ShortURL($data);

if ($this->beforeCreateCallback) {
if ($this->beforeCreateCallback instanceof \Closure) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to make up my mind about whether I like this change. The beforeCreateCallback property can only be null or an instance of Closure. So I'm not sure how much value the additional instanceof check is adding other than making it more explicit.

But if there's a good reason for it, I'm more than happy to be persuaded that this will be a beneficial change 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a more strict check.
This is introduced by the rulesetCodeQuality i set in the rector.php config.
The CodeQuality ruleset includes the two applied rules: FlipTypeControlToUseExclusiveTypeRector https://getrector.com/rule-detail/flip-type-control-to-use-exclusive-type-rector and ExplicitBoolCompareRector https://getrector.com/rule-detail/explicit-bool-compare-rector.

If you want to be more granular in the configuration, to control rules and not generic ruleset, you can use withRules instead of withPreparedSets

value($this->beforeCreateCallback, $shortURL);
}

Expand Down Expand Up @@ -485,7 +485,7 @@ private function setOptions(): void
$this->urlKey = $this->keyGenerator->generateKeyUsing($this->generateKeyUsing);
}

if (! $this->activateAt) {
if (!$this->activateAt instanceof \Carbon\Carbon) {
$this->activateAt = Carbon::now();
}

Expand Down
7 changes: 1 addition & 6 deletions src/Classes/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ protected function shouldAllowAccess(ShortURL $shortURL): bool
if (now()->isBefore($shortURL->activated_at)) {
return false;
}

if ($shortURL->deactivated_at && now()->isAfter($shortURL->deactivated_at)) {
return false;
}

return true;
return !($shortURL->deactivated_at && now()->isAfter($shortURL->deactivated_at));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Classes/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function random_url_key_is_generated_if_one_is_not_explicitly_defined():
public function short_url_can_be_created_and_stored_in_the_database(): void
{
$builder = app(Builder::class);
$shortURL = $builder->destinationUrl('http://domain.com')
$builder->destinationUrl('http://domain.com')
->urlKey('customKey')
->secure()
->trackVisits(false)
Expand Down Expand Up @@ -552,7 +552,7 @@ public function data_can_be_overridden_on_model_using_make_callback(): void
{
$shortUrl = app(Builder::class)
->destinationUrl('https://foo.com')
->beforeCreate(function (ShortURL $shortURL) {
->beforeCreate(function (ShortURL $shortURL): void {
$shortURL->destination_url = 'https://bar.com';
})
->make();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Classes/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public function referer_url_is_stored_if_it_is_enabled(): void

$request = Request::create(config('short-url.default_url').'/short/12345', 'GET', [], [], [], [
'HTTP_referer' => 'https://google.com',
'HTTP_USER_AGENT' => static::trackingFieldsProvider()[1][0],
'HTTP_USER_AGENT' => self::trackingFieldsProvider()[1][0],
]);

$resolver = app(Resolver::class);
Expand Down Expand Up @@ -454,7 +454,7 @@ public function fields_are_not_recorded_if_all_are_true_but_track_visits_is_disa

$request = Request::create(config('short-url.default_url').'/short/12345', 'GET', [], [], [], [
'HTTP_referer' => 'https://google.com',
'HTTP_USER_AGENT' => static::trackingFieldsProvider()[1][0],
'HTTP_USER_AGENT' => self::trackingFieldsProvider()[1][0],
]);

$resolver = app(Resolver::class);
Expand Down
7 changes: 1 addition & 6 deletions tests/Unit/Controllers/ShortURLControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ public function event_is_dispatched_when_the_short_url_is_visited(): void
if ($shortURL->toArray() != $event->shortURL->fresh()->toArray()) {
return false;
}

if ($visit->toArray() != $event->shortURLVisit->fresh()->toArray()) {
return false;
}

return true;
return $visit->toArray() == $event->shortURLVisit->fresh()->toArray();
});
}

Expand Down
Loading