Skip to content
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
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"source": "https://github.com/exoticcaengineering/php-coding-style"
},
"require": {
"symplify/easy-coding-standard": "^12.1"
"php": "^8.3",
"symplify/easy-coding-standard": "^12.5"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"vimeo/psalm": "^5.24"
"phpstan/phpstan": "^2.1",
"vimeo/psalm": "^6.10"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion sets/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
return ECSConfig
::configure()
->withPhpCsFixerSets(
php80Migration: true,
php83Migration: true,
phpCsFixer: true,
)
->withRules([
Expand Down
5 changes: 4 additions & 1 deletion src/CodingStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Exoticca;

/**
* @psalm-suppress UnusedClass
*/
final class CodingStyle
{
public const DEFAULT = __DIR__.'/../sets/default.php';
public const string DEFAULT = __DIR__.'/../sets/default.php';
}
15 changes: 14 additions & 1 deletion src/CodingStyle/Rules/DeclareStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,61 @@

namespace Exoticca\CodingStyle\Rules;

use Override;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer as GlobalDeclareStrictTypesFixer;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class DeclareStrictTypesFixer extends AbstractFixer
{
public GlobalDeclareStrictTypesFixer $globalFixer;
private readonly GlobalDeclareStrictTypesFixer $globalFixer;

/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct()
{
parent::__construct();

$this->globalFixer = new GlobalDeclareStrictTypesFixer();
}

#[Override]
public function getDefinition(): FixerDefinitionInterface
{
return $this->globalFixer->getDefinition();
}

#[Override]
public function getName(): string
{
return 'Exoticca/declare_strict_types';
}

#[Override]
public function getPriority(): int
{
return $this->globalFixer->getPriority();
}

#[Override]
public function supports(SplFileInfo $file): bool
{
return str_contains($file->getPath(), '/adiona/src');
}

#[Override]
public function isCandidate(Tokens $tokens): bool
{
return $this->globalFixer->isCandidate($tokens);
}

#[Override]
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
{
$this->globalFixer->applyFix($file, $tokens);
Expand Down
50 changes: 33 additions & 17 deletions src/CodingStyle/Rules/InlineVarTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Exoticca\CodingStyle\Rules;

use Override;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
Expand All @@ -12,33 +13,36 @@
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class InlineVarTagFixer extends AbstractFixer
{
#[Override]
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Inline `@var` tags should be included in PHPDoc annotations.',
[
new CodeSample(
<<<'PHP'
<?php
new CodeSample(<<<'PHP'
<?php

// @var string
$foo = $_GET['foo'];

// @var string $bar
$bar = $_GET['bar'];

function bat(): string {
// @var string
$foo = $_GET['foo'];

// @var string $bar
$bar = $_GET['bar'];

function bat(): string {
// @var string
return $_GET['bat'];
}
PHP
),
]
return $_GET['bat'];
}
PHP),
],
);
}

#[Override]
public function getName(): string
{
return 'Exoticca/inline_var_tag_fixer';
Expand All @@ -47,19 +51,27 @@ public function getName(): string
/**
* Must run after SingleLineCommentStyleFixer.
*/
#[Override]
public function getPriority(): int
{
return -40;
}

#[Override]
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
}

#[Override]
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
{
/**
* @var int $index
* @var Token $token
*/
foreach ($tokens as $index => $token) {
/** @psalm-suppress RedundantCondition */
if (!$token->isGivenKind(T_COMMENT)
&& !$token->isGivenKind(T_DOC_COMMENT)) {
continue;
Expand All @@ -71,6 +83,10 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
continue;
}

/** @var int $tokenId */
$tokenId = $token->getId();

/** @var string $content */
$content = preg_replace(
'/
^
Expand All @@ -82,12 +98,12 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$
/xS',
'\1',
trim($comment)
trim($comment),
);

$fixedComment = "/** {$content} */";

$tokens[$index] = new Token([$token->getId(), $fixedComment]);
$tokens[$index] = new Token([$tokenId, $fixedComment]);
}
}
}
36 changes: 23 additions & 13 deletions src/CodingStyle/Rules/ValueObjectImportFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

namespace Exoticca\CodingStyle\Rules;

use Override;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ValueObjectImportFixer extends AbstractFixer
{
private const CLASS_NAME_REGEX = '/\b[A-Z][a-zA-Z]+\b/S';
private const ALLOWED_VALUES_REGEX = '/(?:public|protected|private)\s+array\s+\$allowedValues\s=\s\[/S';
private const string CLASS_NAME_REGEX = '/\b[A-Z][a-zA-Z]+\b/S';
private const string ALLOWED_VALUES_REGEX = '/(?:public|protected|private)\s+array\s+\$allowedValues\s=\s\[/S';

private const VALUE_OBJECTS_REPLACEMENTS = [
private const array VALUE_OBJECTS_REPLACEMENTS = [
'BoolValueObject' => 'BooleanValueObject',
'DateTimeValueObject' => 'DateTimeValueObject',
'EmailType' => 'EmailValueObject',
Expand All @@ -33,41 +37,45 @@ final class ValueObjectImportFixer extends AbstractFixer
'StringValueObject' => 'StringValueObject',
];

#[Override]
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Replace old value objects and exceptions with new ones.',
[
new CodeSample(
<<<'PHP'
<?php
new CodeSample(<<<'PHP'
<?php

use Exoticca\Domain\ValueObject\DateTimeValueObject;
use Exoticca\Domain\ValueObject\DateTimeValueObject;

new DateTimeValueObject();
PHP
),
new DateTimeValueObject();
PHP),
]
);
}

#[Override]
public function getName(): string
{
return 'Exoticca/value_object_import_fixer';
}

#[Override]
public function supports(SplFileInfo $file): bool
{
return str_contains($file->getPath(), '/adiona/src');
}

#[Override]
public function isCandidate(Tokens $tokens): bool
{
return true;
}

#[Override]
protected function applyFix(SplFileInfo $file, Tokens $tokens): void
{
/** @var string $originalCode */
$originalCode = file_get_contents($file->getPathname());

$fixedCode = $this->replaceValueObjects($originalCode);
Expand All @@ -82,25 +90,27 @@ private function replaceValueObjects(string $originalCode): string
$fixedCode = str_replace(
search: ' Exoticca\Domain\ValueObject\\',
replace: ' Exoticca\Shared\Domain\ValueObject\\',
subject: $originalCode
subject: $originalCode,
);

if ($fixedCode === $originalCode) {
return $fixedCode;
}

/** @var string $fixedCode */
$fixedCode = preg_replace_callback(
pattern: self::CLASS_NAME_REGEX,
callback: fn (array $matches): string => (
self::VALUE_OBJECTS_REPLACEMENTS[$matches[0]] ?? $matches[0]
),
subject: $fixedCode
subject: $fixedCode,
);

/** @var string */
return preg_replace(
pattern: self::ALLOWED_VALUES_REGEX,
replacement: 'protected static array $allowedValues = [',
subject: $fixedCode
subject: $fixedCode,
);
}
}