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

refactor: fix phpstan errors #170

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ workflows:
- tests:
matrix:
parameters:
php-version: ["7.4", "8.0", "8.1"]
php-version: ["8.0", "8.1"]
- tests-with-future-mode:
matrix:
parameters:
php-version: ["7.4", "8.0", "8.1"]
php-version: ["8.0", "8.1"]
- tests-with-lowest-dependencies:
matrix:
parameters:
php-version: ["7.4", "8.0", "8.1"]
php-version: ["8.0", "8.1"]
- release-test
- release:
requires:
Expand Down
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
->setRules(
RuleSetFactory::create()
->phpCsFixer(true)
->php(7.4, true)
->php(8.0, true)
->pedrotroller(true)
->enable('ordered_imports')
->enable('ordered_interfaces')
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"description": "PHP-CS-FIXER : my custom fixers",
"license": "MIT",
"require": {
"php": "^7.4 || ^8.0",
"friendsofphp/php-cs-fixer": "^3.7"
"php": "^8.0",
"friendsofphp/php-cs-fixer": "^3.9"
},
"require-dev": {
"phpspec/phpspec": "^7.0",
"phpstan/phpstan": "^1.8",
"twig/twig": "^3.3",
"webmozart/assert": "^1.10"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
- tests
6 changes: 4 additions & 2 deletions src/PedroTroller/CS/Fixer/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getName(): string
}

/**
* @return array<null|array>
* @return array<?array<string, mixed>>
*/
public function getSampleConfigurations(): array
{
Expand All @@ -45,7 +45,7 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
$this->getDocumentation(),
array_map(
fn (array $configutation = null) => new CodeSample($this->getSampleCode(), $configutation),
fn (?array $configutation): CodeSample => new CodeSample($this->getSampleCode(), $configutation),
$this->getSampleConfigurations()
)
);
Expand Down Expand Up @@ -76,6 +76,8 @@ protected function hasUseStatements(Tokens $tokens, $fqcn): bool

/**
* @param string|string[] $fqcn
*
* @return null|array<int, Token>
*/
protected function getUseStatements(Tokens $tokens, $fqcn): ?array
{
Expand Down
83 changes: 60 additions & 23 deletions src/PedroTroller/CS/Fixer/AbstractOrderedClassElementsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PedroTroller\CS\Fixer;

use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;

Expand Down Expand Up @@ -36,18 +37,49 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
}

/**
* @param array[] $elements
* @param array<
* array{
* start: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type?: string,
* methodName?: string,
* propertyName?: string,
* end?: int
* }
* > $elements
*
* @return array[]
* @return array<
* array{
* start: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type?: string,
* methodName?: string,
* propertyName?: string,
* end?: int
* }
* >
*/
abstract protected function sortElements(array $elements): array;

/**
* @param int $startIndex
*
* @return array[]
* @return array<
* array{
* start: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type?: string,
* methodName?: string,
* propertyName?: string,
* end?: int
* }
* >
*/
private function getElements(Tokens $tokens, $startIndex)
private function getElements(Tokens $tokens, int $startIndex)
{
static $elementTokenKinds = [CT::T_USE_TRAIT, T_CONST, T_VARIABLE, T_FUNCTION];

Expand All @@ -59,6 +91,7 @@ private function getElements(Tokens $tokens, $startIndex)
'start' => $startIndex,
'visibility' => 'public',
'static' => false,
'comment' => null,
];

for ($i = $startIndex;; ++$i) {
Expand Down Expand Up @@ -98,6 +131,7 @@ private function getElements(Tokens $tokens, $startIndex)

break;
}

$element['end'] = $this->findElementEnd($tokens, $i);

break;
Expand All @@ -107,8 +141,6 @@ private function getElements(Tokens $tokens, $startIndex)

if (isset($tokens[$possibleCommentIndex]) && $tokens[$possibleCommentIndex]->isComment()) {
$element['comment'] = $tokens[$possibleCommentIndex]->getContent();
} else {
$element['comment'] = null;
}

$elements[] = $element;
Expand All @@ -117,11 +149,9 @@ private function getElements(Tokens $tokens, $startIndex)
}

/**
* @param int $index
*
* @return array|string type or array of type and name
* @return array{string, string}|string
*/
private function detectElementType(Tokens $tokens, $index)
private function detectElementType(Tokens $tokens, int $index)
{
$token = $tokens[$index];

Expand Down Expand Up @@ -165,12 +195,7 @@ private function detectElementType(Tokens $tokens, $index)
return 'method';
}

/**
* @param int $index
*
* @return int
*/
private function findElementEnd(Tokens $tokens, $index)
private function findElementEnd(Tokens $tokens, int $index): int
{
$index = $tokens->getNextTokenOfKind($index, ['{', ';']);

Expand All @@ -186,16 +211,28 @@ private function findElementEnd(Tokens $tokens, $index)
}

/**
* @param int $startIndex
* @param int $endIndex
* @param array[] $elements
* @param array<
* array{
* start: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type?: string,
* methodName?: string,
* propertyName?: string,
* end?: int
* }
* > $elements
*/
private function sortTokens(
Tokens $tokens,
$startIndex,
$endIndex,
int $startIndex,
int $endIndex,
array $elements
): void {
/**
* @var Token[]
*/
$replaceTokens = [];

foreach ($elements as $element) {
Expand Down
2 changes: 1 addition & 1 deletion src/PedroTroller/CS/Fixer/Behat/OrderBehatStepsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function sortElements(array $elements): array
}

foreach ($elements as $index => $element) {
if ('method' !== $element['type']) {
if (false === isset($element['type']) || 'method' !== $element['type']) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ protected function sortElements(array $elements): array
return $result;
}

/**
* @param array<
* array{
* start: int,
* end: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type: string,
* methodName?: string,
* propertyName?: string
* }
* > $elements
*
* @return string[]
*/
private function getMethodsNames(array $elements): array
{
$methods = [];
Expand All @@ -162,6 +178,22 @@ private function getMethodsNames(array $elements): array
return $methods;
}

/**
* @param array<
* array{
* start: int,
* end: int,
* visibility: string,
* static: bool,
* comment: ?string,
* type: string,
* methodName?: string,
* propertyName?: string
* }
* > $elements
*
* @return string[]
*/
private function getPropertiesNames(array $elements): array
{
$properties = array_filter($elements, fn ($element) => 'property' === $element['type']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$keys = array_keys($case);
array_pop($keys);
array_pop($case);
$tokens[end($keys)] = $this->cleanupMessage(end($case));

$key = end($keys);
$token = end($case);

if (false === $key || false === $token || false === \is_int($key)) {
continue;
}

$tokens[$key] = $this->cleanupMessage($token);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
}
}

private function splitArgs(Tokens $tokens, $index): void
private function splitArgs(Tokens $tokens, int $index): void
{
$this->mergeArgs($tokens, $index);

Expand Down Expand Up @@ -211,7 +211,7 @@ private function splitArgs(Tokens $tokens, $index): void
}
}

private function mergeArgs(Tokens $tokens, $index): void
private function mergeArgs(Tokens $tokens, int $index): void
{
$openBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
$closeBraceIndex = $this->analyze($tokens)->getClosingParenthesis($openBraceIndex);
Expand All @@ -231,7 +231,7 @@ private function mergeArgs(Tokens $tokens, $index): void
}
}

private function localizeNextCloseBrace(Tokens $tokens, $index)
private function localizeNextCloseBrace(Tokens $tokens, int $index): int
{
$opening = 0;

Expand All @@ -252,7 +252,7 @@ private function localizeNextCloseBrace(Tokens $tokens, $index)
return 0;
}

private function localizeNextCloseBracket(Tokens $tokens, $index)
private function localizeNextCloseBracket(Tokens $tokens, int $index): int
{
$opening = 0;

Expand All @@ -273,7 +273,7 @@ private function localizeNextCloseBracket(Tokens $tokens, $index)
return 0;
}

private function getNumberOfArguments(Tokens $tokens, $index)
private function getNumberOfArguments(Tokens $tokens, int $index): int
{
if (T_FUNCTION !== $tokens[$index]->getId()) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
}
}

/**
* @param array<int, Token> $matchedTokens
*/
private function handleDo(array $matchedTokens, Tokens $tokens): void
{
foreach ($matchedTokens as $index => $token) {
Expand All @@ -77,6 +80,9 @@ private function handleDo(array $matchedTokens, Tokens $tokens): void
}
}

/**
* @param array<int, Token> $matchedTokens
*/
private function handleCommon(array $matchedTokens, Tokens $tokens): void
{
foreach ($matchedTokens as $index => $token) {
Expand All @@ -88,12 +94,7 @@ private function handleCommon(array $matchedTokens, Tokens $tokens): void
continue;
}

$openCurlyBracket = current(array_keys($curlyBracket));

if (false === $openCurlyBracket) {
continue;
}

$openCurlyBracket = current(array_keys($curlyBracket));
$closeCurlyBracket = $this->analyze($tokens)->getClosingCurlyBracket($openCurlyBracket);

if (null === $closeCurlyBracket) {
Expand All @@ -107,7 +108,7 @@ private function handleCommon(array $matchedTokens, Tokens $tokens): void
}
}

private function fixSpaces($index, Tokens $tokens): void
private function fixSpaces(int $index, Tokens $tokens): void
{
$space = $index + 1;

Expand All @@ -128,7 +129,7 @@ private function fixSpaces($index, Tokens $tokens): void
$tokens[$space] = new Token([T_WHITESPACE, $this->ensureNumberOfBreaks($tokens[$space]->getContent())]);
}

private function ensureNumberOfBreaks($whitespace)
private function ensureNumberOfBreaks(string $whitespace): string
{
$parts = explode("\n", $whitespace);

Expand Down
Loading