Skip to content

Commit

Permalink
Drop PHP 7.4 support, Fix Doctrine Entities by Attributes (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed May 31, 2022
1 parent 57909e2 commit 85e2263
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 28 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout"
Expand Down Expand Up @@ -62,8 +62,8 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"
- "8.1"

steps:
- name: "Checkout"
Expand All @@ -72,7 +72,7 @@ jobs:
- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "xdebug"
coverage: "pcov"
php-version: "${{ matrix.php-version }}"
ini-values: zend.assertions=1

Expand All @@ -95,6 +95,7 @@ jobs:
run: "vendor/bin/phpunit --coverage-clover=coverage.xml"

- name: "Send code coverage report to Codecov.io"
if: ${{ matrix.php-version == '8.1' }}
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
Expand All @@ -109,7 +110,7 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout"
Expand Down Expand Up @@ -146,7 +147,7 @@ jobs:
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

steps:
- name: "Checkout"
Expand Down
28 changes: 16 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "~8.0.0 || ~8.1.0",
"ext-mbstring": "*",
"ext-tokenizer": "*",
"friendsofphp/php-cs-fixer": "^3.1.0"
"friendsofphp/php-cs-fixer": "^3.8.0"
},
"require-dev": {
"malukenho/mcbumpface": "^1.1.5",
"phpstan/phpstan": "^0.12.99",
"phpstan/phpstan-phpunit": "^0.12.22",
"phpunit/phpunit": "^9.5.9",
"phpstan/phpstan": "^1.7.6",
"phpstan/phpstan-phpunit": "^1.1.1",
"phpunit/phpunit": "^9.5.20",
"slam/php-debug-r": "^1.7.0",
"slam/phpstan-extensions": "^5.1.0",
"thecodingmachine/phpstan-strict-rules": "^0.12.1"
},
"extra": {
"mc-bumpface": {
"stripVersionPrefixes": true
}
"slam/phpstan-extensions": "^6.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -38,5 +32,15 @@
"psr-4": {
"SlamCsFixer\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"malukenho/mcbumpface": true
}
},
"extra": {
"mc-bumpface": {
"stripVersionPrefixes": true
}
}
}
4 changes: 3 additions & 1 deletion lib/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class Config extends PhpCsFixerConfig
{
public const RULES = [
'@DoctrineAnnotation' => true,
'@PHP80Migration' => true,
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'@PHPUnit84Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
Expand All @@ -29,6 +29,8 @@ final class Config extends PhpCsFixerConfig
'combine_consecutive_unsets' => false,
'comment_to_phpdoc' => false,
'concat_space' => ['spacing' => 'one'],
'control_structure_continuation_position' => true,
'date_time_create_from_format_call' => true,
'date_time_immutable' => false,
'declare_parentheses' => true,
'error_suppression' => false,
Expand Down
31 changes: 29 additions & 2 deletions lib/FinalInternalClassFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
Expand Down Expand Up @@ -47,8 +48,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
}

// ignore class if it's a Doctrine Entity
$docToken = $tokens[$tokens->getPrevNonWhitespace($classIndex)];
if ($docToken->isGivenKind(\T_DOC_COMMENT) && false !== \mb_strpos($docToken->getContent(), '@ORM\Entity')) {
if (self::isDoctrineEntity($tokens, $classIndex)) {
continue;
}

Expand All @@ -61,4 +61,31 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
);
}
}

private static function isDoctrineEntity(Tokens $tokens, int $classIndex): bool
{
$docToken = $tokens[$tokens->getPrevNonWhitespace($classIndex)];
if ($docToken->isGivenKind(\T_DOC_COMMENT) && false !== \mb_strpos($docToken->getContent(), '@ORM\Entity')) {
return true;
}

while ($classIndex > 0 && $tokens[$tokens->getPrevNonWhitespace($classIndex)]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) {
$attributeOpenIndex = $tokens->getPrevTokenOfKind($classIndex, [[\T_ATTRIBUTE]]);
\assert(null !== $attributeOpenIndex);
$content = '';
for ($index = $attributeOpenIndex; $index < $classIndex; ++$index) {
$content .= $tokens[$index]->getContent();
}
if (false !== \mb_strpos($content, '#[ORM\Entity]')) {
return true;
}
if (false !== \mb_strpos($content, '#[\Doctrine\ORM\Mapping\Entity')) {
return true;
}

$classIndex = $attributeOpenIndex - 1;
}

return false;
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
includes:
- phar://phpstan.phar/conf/config.level5.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/slam/phpstan-extensions/conf/slam-rules.neon

parameters:
level: 5
paths:
- lib/
- tests/
2 changes: 1 addition & 1 deletion tests/AbstractFixerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final protected function doTest($expected, $input = null, ?SplFileInfo $file = n

Tokens::clearCache();
$expectedTokens = Tokens::fromCode($expected);
static::assertTokens($expectedTokens, $tokens);
self::assertTokens($expectedTokens, $tokens);
}

static::assertNull($this->lintSource($expected));
Expand Down
3 changes: 1 addition & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
*/
final class ConfigTest extends TestCase
{
private ?array $setDefinitions;

public function testConfig(): void
{
$config = new Config();
Expand All @@ -32,6 +30,7 @@ public function testConfig(): void
public function testAllRulesAreSpecifiedAndDifferentFromRuleSets(): void
{
$config = new Config();

/** @var array<string, mixed> $configRules */
$configRules = $config->getRules();
$ruleSet = new RuleSet($configRules);
Expand Down
9 changes: 9 additions & 0 deletions tests/FinalInternalClassFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public function provideCases(): array
[
"<?php\n/**\n * @ORM\\Entity\n */\nclass MyEntity {}",
],
[
"<?php\n#[ORM\\Entity]\nclass MyEntity {}",
],
[
"<?php\n#[ORM\\Entity]\n#[CustomAttribute]\nclass MyEntity {}",
],
[
"<?php\n#[\\Doctrine\\ORM\\Mapping\\Entity(repositoryClass: \\MyClass::class)]\nclass MyEntity {}",
],
[
'<?php abstract class MyAbstract {}',
],
Expand Down
8 changes: 4 additions & 4 deletions tests/PhpFileOnlyProxyFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public function testGetDefinitionIsProxied(): void

$proxy = new PhpFileOnlyProxyFixer($fixer);

$fixerDefinition = $this->createMock(FixerDefinitionInterface::class);
$fixerDefinition->expects(self::once())->method('getSummary')->willReturn($summary = \uniqid('summary'));
$fixerDefinition->expects(self::once())->method('getCodeSamples')->willReturn($codeSamples = []);
$fixerDefinition->expects(self::once())->method('getDescription')->willReturn($description = \uniqid('description'));
$fixerDefinition = $this->createMock(FixerDefinitionInterface::class);
$fixerDefinition->expects(self::once())->method('getSummary')->willReturn($summary = \uniqid('summary'));
$fixerDefinition->expects(self::once())->method('getCodeSamples')->willReturn($codeSamples = []);
$fixerDefinition->expects(self::once())->method('getDescription')->willReturn($description = \uniqid('description'));
$fixerDefinition->expects(self::once())->method('getRiskyDescription')->willReturn($riskyDescription = \uniqid('riskyDescription'));

$fixer
Expand Down

0 comments on commit 85e2263

Please sign in to comment.