Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3798-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3799-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)
Expand Down
94 changes: 51 additions & 43 deletions src/Fixer/StringableInterfaceFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -60,11 +62,21 @@ public function isRisky(): bool

public function fix(\SplFileInfo $file, Tokens $tokens): void
{
$namespaceStartIndex = 0;
$useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens);

$stringableInterfaces = ['stringable'];

for ($index = 1; $index < $tokens->count(); $index++) {
if ($tokens[$index]->isGivenKind(\T_NAMESPACE)) {
$namespaceStartIndex = $index;
$stringableInterfaces = [];
continue;
}

if ($tokens[$index]->isGivenKind(\T_USE)) {
$name = self::getNameFromUse($index, $useDeclarations);
if ($name !== null) {
$stringableInterfaces[] = $name;
}
continue;
}

Expand All @@ -81,14 +93,36 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

if (self::doesImplementStringable($tokens, $namespaceStartIndex, $index, $classStartIndex)) {
if (self::doesImplementStringable($tokens, $index, $classStartIndex, $stringableInterfaces)) {
continue;
}

self::addStringableInterface($tokens, $index);
}
}

/**
* @param list<NamespaceUseAnalysis> $useDeclarations
*/
private static function getNameFromUse(int $index, array $useDeclarations): ?string
{
$uses = \array_filter(
$useDeclarations,
static fn (NamespaceUseAnalysis $namespaceUseAnalysis): bool => $namespaceUseAnalysis->getStartIndex() === $index,
);

\assert(\count($uses) === 1);

$useDeclaration = \reset($uses);

$lowercasedFullName = \strtolower($useDeclaration->getFullName());
if ($lowercasedFullName !== 'stringable' && $lowercasedFullName !== '\\stringable') {
return null;
}

return \strtolower($useDeclaration->getShortName());
}

private static function doesHaveToStringMethod(Tokens $tokens, int $classStartIndex, int $classEndIndex): bool
{
$index = $classStartIndex;
Expand All @@ -115,23 +149,25 @@ private static function doesHaveToStringMethod(Tokens $tokens, int $classStartIn
return false;
}

private static function doesImplementStringable(Tokens $tokens, int $namespaceStartIndex, int $classKeywordIndex, int $classOpenBraceIndex): bool
{
$interfaces = self::getInterfaces($tokens, $classKeywordIndex, $classOpenBraceIndex);
if ($interfaces === []) {
/**
* @param list<string> $stringableInterfaces
*/
private static function doesImplementStringable(
Tokens $tokens,
int $classKeywordIndex,
int $classOpenBraceIndex,
array $stringableInterfaces
): bool {
$implementedInterfaces = self::getInterfaces($tokens, $classKeywordIndex, $classOpenBraceIndex);
if ($implementedInterfaces === []) {
return false;
}

if (\in_array('\\stringable', $interfaces, true)) {
if (\in_array('\\stringable', $implementedInterfaces, true)) {
return true;
}

if ($namespaceStartIndex === 0 && \in_array('stringable', $interfaces, true)) {
return true;
}

foreach (self::getImports($tokens, $namespaceStartIndex, $classKeywordIndex) as $import) {
if (\in_array($import, $interfaces, true)) {
foreach ($stringableInterfaces as $stringableInterface) {
if (\in_array($stringableInterface, $implementedInterfaces, true)) {
return true;
}
}
Expand Down Expand Up @@ -170,34 +206,6 @@ private static function getInterfaces(Tokens $tokens, int $classKeywordIndex, in
return $interfaces;
}

/**
* @return iterable<string>
*/
private static function getImports(Tokens $tokens, int $namespaceStartIndex, int $classKeywordIndex): iterable
{
for ($index = $namespaceStartIndex; $index < $classKeywordIndex; $index++) {
if (!$tokens[$index]->isGivenKind(\T_USE)) {
continue;
}
$nameIndex = $tokens->getNextMeaningfulToken($index);
\assert(\is_int($nameIndex));

if ($tokens[$nameIndex]->isGivenKind(\T_NS_SEPARATOR)) {
$nameIndex = $tokens->getNextMeaningfulToken($nameIndex);
\assert(\is_int($nameIndex));
}

$nextIndex = $tokens->getNextMeaningfulToken($nameIndex);
\assert(\is_int($nextIndex));
if ($tokens[$nextIndex]->isGivenKind(\T_AS)) {
$nameIndex = $tokens->getNextMeaningfulToken($nextIndex);
\assert(\is_int($nameIndex));
}

yield \strtolower($tokens[$nameIndex]->getContent());
}
}

private static function addStringableInterface(Tokens $tokens, int $classIndex): void
{
$implementsIndex = $tokens->getNextTokenOfKind($classIndex, ['{', [\T_IMPLEMENTS]]);
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixer/StringableInterfaceFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ public function __toString() { return "Foo"; }
}
';

yield [
<<<'PHP'
<?php
use NotStringable as Stringy;
class Bar implements \Stringable, Stringy {
public function __toString() { return ""; }
}
PHP,
<<<'PHP'
<?php
use NotStringable as Stringy;
class Bar implements Stringy {
public function __toString() { return ""; }
}
PHP,
];

$implementedInterfacesCases = [
\Stringable::class,
'Foo\\Stringable',
Expand Down