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

Make php artisan enum:to-native compatible with rector 0.19 #345

Merged
merged 2 commits into from
Jan 12, 2024
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.0, 8.1]
php: [8.0, 8.1, 8.2, 8.3]
dependency-versions: [lowest, highest]

name: Tests - P${{ matrix.php }} - ${{ matrix.dependency-versions }} - ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
Expand All @@ -41,13 +41,13 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.0, 8.1]
php: [8.0, 8.1, 8.2, 8.3]
dependency-versions: [lowest, highest]

name: Static Analysis - P${{ matrix.php }} - ${{ matrix.dependency-versions }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 6.8.0

### Changed

- Make `php artisan enum:to-native` compatible with rector `0.19`

## 6.7.0

### Added
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@
"ergebnis/composer-normalize": "^2.28.3",
"mll-lab/php-cs-fixer-config": "^5.4",
"mockery/mockery": "^1.5",
"nunomaduro/larastan": "^2.6.3",
"larastan/larastan": "^2.6.3",
"orchestra/testbench": "^7.6.1 || ^8",
"phpstan/phpstan": "^1.8.2",
"phpstan/extension-installer": "^1",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-phpunit": "^1.1.1",
"phpunit/phpunit": "^9.5.21 || ^10",
"rector/rector": "^0.17.6",
"symplify/rule-doc-generator": "^11"
"rector/rector": "^0.19",
"symplify/rule-doc-generator": "^11 || ^12"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -59,7 +60,8 @@
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"sort-packages": true
},
Expand Down
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
includes:
- extension.neon
- vendor/nunomaduro/larastan/extension.neon
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-mockery/extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
parameters:
level: 6 # TODO level up to max
paths:
Expand Down
36 changes: 0 additions & 36 deletions rector-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,6 @@ Convert usages of `BenSampo\Enum\Enum` to native PHP enums

- class: [`BenSampo\Enum\Rector\ToNativeImplementationRector`](src/Rector/ToNativeImplementationRector.php)

```php
<?php

declare(strict_types=1);

use BenSampo\Enum\Rector\ToNativeImplementationRector;
use BenSampo\Enum\Tests\Enums\UserType;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ToNativeImplementationRector::class, [
UserType::class,
]);
};
```


```diff
-/**
- * @method static static ADMIN()
Expand Down Expand Up @@ -53,24 +35,6 @@ Convert usages of `BenSampo\Enum\Enum` to native PHP enums

- class: [`BenSampo\Enum\Rector\ToNativeUsagesRector`](src/Rector/ToNativeUsagesRector.php)

```php
<?php

declare(strict_types=1);

use BenSampo\Enum\Rector\ToNativeUsagesRector;
use BenSampo\Enum\Tests\Enums\UserType;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(ToNativeUsagesRector::class, [
UserType::class,
]);
};
```


```diff
-$user = UserType::ADMIN();
-$user->is(UserType::ADMIN);
Expand Down
2 changes: 0 additions & 2 deletions src/Attributes/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace BenSampo\Enum\Attributes;

use Attribute;

#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS)]
class Description
{
Expand Down
2 changes: 1 addition & 1 deletion src/PHPStan/UniqueValuesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function processNode(Node $node, Scope $scope): array
foreach ($constants as $name => $value) {
$constantsWithValue = array_filter($constants, fn (mixed $v): bool => $v === $value);
if (count($constantsWithValue) > 1) {
$duplicateConstants []= array_keys($constantsWithValue);
$duplicateConstants[] = array_keys($constantsWithValue);
}
}
$duplicateConstants = array_unique($duplicateConstants);
Expand Down
8 changes: 5 additions & 3 deletions src/Rector/ToNativeImplementationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \BenSampo\Enum\Tests\Rector\ToNativeRectorImplementationTest
*/
/** @see \BenSampo\Enum\Tests\Rector\ToNativeRectorImplementationTest */
class ToNativeImplementationRector extends ToNativeRector
{
public function __construct(
protected PhpDocInfoPrinter $phpDocInfoPrinter,
protected PhpDocInfoFactory $phpDocInfoFactory,
protected ValueResolver $valueResolver,
) {}

public function getRuleDefinition(): RuleDefinition
Expand Down
4 changes: 2 additions & 2 deletions src/Rector/ToNativeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;

/**
* Conversion of enums and their usages can not be done in a single run of Rector,
Expand Down
4 changes: 1 addition & 3 deletions src/Rector/ToNativeUsagesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \BenSampo\Enum\Tests\Rector\ToNativeRectorUsagesTest
*/
/** @see \BenSampo\Enum\Tests\Rector\ToNativeRectorUsagesTest */
class ToNativeUsagesRector extends ToNativeRector
{
public const COMPARED_AGAINST_ENUM_INSTANCE = ToNativeUsagesRector::class . '@compared-against-enum-instance';
Expand Down
20 changes: 14 additions & 6 deletions tests/Rector/Usages/assign.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@

use BenSampo\Enum\Tests\Enums\UserType;

/** @var UserType|null $maybeUserType */

class Foo {
public UserType $userType;
}

/** @var UserType|null $maybeUserType */
$foo = $maybeUserType ?? UserType::Administrator();
(new Foo)->userType = UserType::Administrator();
$foo = new Foo;
$foo->userType = UserType::Administrator();
/** @var UserType $userType */
$userType = UserType::Administrator();

$qux = UserType::Administrator;
$bar ??= UserType::Administrator;
$baz .= UserType::Administrator;
$ref &= UserType::Administrator;
(new Foo)->userType = UserType::Administrator;
-----
<?php

use BenSampo\Enum\Tests\Enums\UserType;

/** @var UserType|null $maybeUserType */

class Foo {
public UserType $userType;
}

/** @var UserType|null $maybeUserType */
$foo = $maybeUserType ?? UserType::Administrator;
(new Foo)->userType = UserType::Administrator;
$foo = new Foo;
$foo->userType = UserType::Administrator;
/** @var UserType $userType */
$userType = UserType::Administrator;

$qux = UserType::Administrator->value;
$bar ??= UserType::Administrator->value;
$baz .= UserType::Administrator->value;
$ref &= UserType::Administrator->value;
(new Foo)->userType = UserType::Administrator;