Skip to content

Commit

Permalink
cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed Mar 20, 2020
1 parent 3adc34e commit 74c0e80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
29 changes: 13 additions & 16 deletions src/Fixer/Preload/ExplicitlyLoadClass.php
Expand Up @@ -13,16 +13,12 @@
namespace PhpCsFixer\Fixer\Preload;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

/**
*
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ExplicitlyLoadClass extends AbstractFixer
Expand Down Expand Up @@ -57,11 +53,11 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)

$classesToLoad = array_diff($candidates, $classesNotToLoad);
$this->injectClasses($tokens, $classesToLoad);

}

/**
* @var string $functionName
* @param string $functionName
*
* @return string[] classes
*/
private function parse(Tokens $tokens, $functionName)
Expand All @@ -70,14 +66,15 @@ private function parse(Tokens $tokens, $functionName)
$index = $this->findFunction($tokens, $functionName);
// if not public, get the types.
// TODO there may be other keyword but public/private/protected, eg "static"
if ($tokens[$tokens->getPrevMeaningfulToken($index)]->getContent() !== 'public') {
if ('public' !== $tokens[$tokens->getPrevMeaningfulToken($index)]->getContent()) {
// Get argument types
$startedParsingArguments = false;
for ($i = $index; $i < count($tokens); $i++) {
for ($i = $index; $i < \count($tokens); ++$i) {
$token = $tokens[$i];
// Look for when the arguments begin
if ($token->getContent() === '(') {
if ('(' === $token->getContent()) {
$startedParsingArguments = true;

continue;
}

Expand All @@ -87,11 +84,11 @@ private function parse(Tokens $tokens, $functionName)
}

// Are all arguments parsed?
if ($token->getContent() === ')') {
if (')' === $token->getContent()) {
break;
}

if ($token->isGivenKind(T_STRING) && !$token->isKeyword() && !in_array($token->getContent(), ['string', 'bool', 'array', 'float', 'int'])) {
if ($token->isGivenKind(T_STRING) && !$token->isKeyword() && !\in_array($token->getContent(), ['string', 'bool', 'array', 'float', 'int'], true)) {
$classes[] = $token->getContent();
}
}
Expand All @@ -117,7 +114,7 @@ private function getPreloadedClasses(\SplFileInfo $file, Tokens $tokens)
}

// TODO rework so it is way better
if ($token->getContent() === 'class_exists') {
if ('class_exists' === $token->getContent()) {
$nextToken = $tokens[$index + 2];
$classes[] = $nextToken->getContent();
}
Expand All @@ -126,13 +123,12 @@ private function getPreloadedClasses(\SplFileInfo $file, Tokens $tokens)
return $classes;
}


/**
* Find a function in the tokens.
*
* @param Tokens $tokens
* @param string $name
* @return int|null the index or null. The index is to the "function" token.
*
* @return null|int the index or null. The index is to the "function" token.
*/
private function findFunction(Tokens $tokens, $name)
{
Expand Down Expand Up @@ -163,10 +159,11 @@ private function injectClasses(Tokens $tokens, array $classes)
}

$insertAfter = $tokens->getPrevMeaningfulToken($index);

break;
}

if ($insertAfter === null) {
if (null === $insertAfter) {
return;
}

Expand Down
7 changes: 3 additions & 4 deletions tests/Fixer/Preload/ExplicitlyLoadClassTest.php
Expand Up @@ -38,10 +38,9 @@ public function testFix($expected, $input = null)

public function provideFixCases()
{
$testDir = dirname(__DIR__, 2).'/Fixtures/Preload';
$testDir = \dirname(__DIR__, 2).'/Fixtures/Preload';
$finder = new Finder();
$finder->in($testDir)
->name('*_Out.php');
$finder->in($testDir)->name('*_Out.php');

/** @var SplFileInfo $file */
foreach ($finder as $file) {
Expand All @@ -54,7 +53,7 @@ public function provideFixCases()
$input = file_get_contents($inputFile);
}

yield $file->getFilename() =>[$output, $input];
yield $file->getFilename() => [$output, $input];
}
}
}

0 comments on commit 74c0e80

Please sign in to comment.