Inspection with PHP-CS-Fixer custom rules.

Hi, I am having trouble trying to figure this out.

I have a custom .php_cs.dist configuration that has always worked just fine.

I have written a custom Php-Cs-Fixer rule, which works on cli, however when I have it present in my .php_cs.dist configuration file I do not see the warnings in PhpStorm.

I am confused, because on the command line, php-cs-fixer is using the same config and it is working fine.

I have also tried to search for info on how inspection works in PhPStorm w/ php-cs-fixer but have found no information.

6 comments
Comment actions Permalink

Could you please share the custom config you are referring to? If it depends on something else, please share this something else too.

0
Comment actions Permalink

.php_cs.dist

<?php

return PhpCsFixer\Config::create()
->setRules([

'@Symfony' => true,
'return_type_declaration' => true,
'Fixer/linebreaksbetweenmethodarguments' => true,
])
->registerCustomFixers([
new App\CodingStandard\Fixer\LineBreakBetweenMethodArgumentsFixer(),
])
->setUsingCache(true);

 

App/CodingStandard/Fixer/LineBreakBetweenMethodArgumentsFixer.php

<?php

declare(strict_types=1);

namespace App\CodingStandard\Fixer;

use App\CodingStandard\Utils\TokensAnalyzer;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\DefinedFixerInterface;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\WhitespacesFixerConfig;
use SplFileInfo;

final class LineBreakBetweenMethodArgumentsFixer implements DefinedFixerInterface, WhitespacesAwareFixerInterface
{
private $whitespacesConfig;

private $configuration = [
'max-args' => 3, //The maximum number of arguments allowed with splitting the arguments into several lines (use `false` to disable this feature)
'max-length' => 80, //The maximum number of characters allowed with splitting the arguments into several lines
'automatic-argument-merge' => true, //If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline
];

public function getName(): string
{
return 'Fixer/linebreaksbetweenmethodarguments';
}

public function getDefinition()
{
return new FixerDefinition(
'If the declaration of a method is too long, the arguments of this method MUST BE separated (one argument per line)',
[
new CodeSample(
'<?php
namespace Project\TheNamespace;
class TheClass
{
public function fun1($arg1, array $arg2 = [], $arg3 = null)
{
return;
}
public function fun2($arg1, array $arg2 = [], \ArrayAccess $arg3 = null, bool $bool = true, \Iterator $thisLastArgument = null)
{
return;
}
public function fun3(
$arg1,
array $arg2 = []
) {
return;
}
}'
),
]
);
}

public function construct()
{
$this->whitespacesConfig = $this->getDefaultWhitespacesFixerConfig();
}

public function isCandidate(Tokens $tokens)
{
return true;
}

public function isRisky(): bool
{
return false;
}

public function supports(SplFileInfo $file): bool
{
return true;
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
$priorities = array_map(
function ($class) {
return (new $class())->getPriority();
},
[BracesFixer::class]
);

return min($priorities) - 1;
}

public function fix(SplFileInfo $file, Tokens $tokens): void
{
$functions = [];

foreach ($tokens as $index => $token) {
if (T_FUNCTION === $token->getId()) {
$functions[$index] = $token;
}
}

foreach (array_reverse($functions, true) as $index => $token) {
$nextIndex = $tokens->getNextMeaningfulToken($index);
$next = $tokens[$nextIndex];

if (null === $nextIndex) {
continue;
}

if (T_STRING !== $next->getId()) {
continue;
}

$openBraceIndex = $tokens->getNextMeaningfulToken($nextIndex);
$openBrace = $tokens[$openBraceIndex];

if (false === $openBrace->equals('(')) {
continue;
}

if (0 === $this->analyze($tokens)->getNumberOfArguments($index)) {
$this->mergeArgs($tokens, $index);

continue;
}

if ($this->analyze($tokens)->getSizeOfTheLine($index) > $this->configuration['max-length']) {
$this->splitArgs($tokens, $index);

continue;
}

if (false !== $this->configuration['max-args'] && $this->analyze($tokens)->getNumberOfArguments($index) > $this->configuration['max-args']) {
$this->splitArgs($tokens, $index);

continue;
}

$clonedTokens = clone $tokens;
$this->mergeArgs($clonedTokens, $index);

if ($this->analyze($clonedTokens)->getSizeOfTheLine($index) > $this->configuration['max-length']) {
$this->splitArgs($tokens, $index);
} elseif ($this->configuration['automatic-argument-merge']) {
$this->mergeArgs($tokens, $index);
}
}
}

private function splitArgs(Tokens $tokens, $index): void
{
$openBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
$closeBraceIndex = $this->analyze($tokens)->getClosingParenthesis($openBraceIndex);

if (0 === $closeBraceIndex) {
return;
}

$token = $tokens[$openBraceIndex];
$tokens[$openBraceIndex] = new Token([
T_WHITESPACE,
trim($token->getContent())."\n".$this->analyze($tokens)->getLineIndentation($index).$this->whitespacesConfig->getIndent(),
]);

$token = $tokens[$closeBraceIndex];
$tokens[$closeBraceIndex] = new Token([
T_WHITESPACE,
rtrim($this->whitespacesConfig->getLineEnding().$this->analyze($tokens)->getLineIndentation($index).$token->getContent()),
]);

if ($tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)]->equals('{')) {
$tokens->removeTrailingWhitespace($closeBraceIndex);
$token = $tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)];
$tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)] = new Token([
T_WHITESPACE,
' '.trim($token->getContent()),
]);
}

if ($tokens[$tokens->getNextMeaningfulToken($closeBraceIndex)]->isGivenKind(CT::T_TYPE_COLON)) {
$end = $tokens->getNextTokenOfKind($closeBraceIndex, [';', '{']);

for ($i = $closeBraceIndex + 1; $i < $end; ++$i) {
$content = preg_replace('/ {2,}/', ' ', str_replace("\n", '', $tokens[$i]->getContent()));
$tokens[$i] = new Token([$tokens[$i]->getId(), $content]);
}
}

for ($i = $openBraceIndex + 1; $i < $closeBraceIndex; ++$i) {
if ($tokens[$i]->equals('(')) {
$i = $this->analyze($tokens)->getClosingParenthesis($i);
}

if ($tokens[$i]->equals('[')) {
$i = $this->analyze($tokens)->getClosingBracket($i);
}

if ($tokens[$i]->equals(',')) {
$tokens->removeTrailingWhitespace($i);
$token = $tokens[$i];
$tokens[$i] = new Token([
T_WHITESPACE,
trim($token->getContent())."\n".$this->analyze($tokens)->getLineIndentation($index).$this->whitespacesConfig->getIndent(),
]);
}
}

$tokens->removeTrailingWhitespace($openBraceIndex);
$tokens->removeTrailingWhitespace($tokens->getPrevMeaningfulToken($closeBraceIndex));
}

private function mergeArgs(Tokens $tokens, $index): void
{
$openBraceIndex = $tokens->getNextTokenOfKind($index, ['(']);
$closeBraceIndex = $this->analyze($tokens)->getClosingParenthesis($openBraceIndex);

for ($i = $openBraceIndex; $i <= $closeBraceIndex; ++$i) {
$content = preg_replace('/ {2,}/', ' ', str_replace("\n", '', $tokens[$i]->getContent()));
$tokens[$i] = $tokens[$i]->getId()
? new Token([$tokens[$i]->getId(), $content])
: new Token([T_WHITESPACE, $content]);
}

$tokens->removeTrailingWhitespace($openBraceIndex);
$tokens->removeLeadingWhitespace($closeBraceIndex);

$end = $tokens->getNextTokenOfKind($closeBraceIndex, [';', '{']);

if ($tokens[$end]->equals('{')) {
$tokens->removeLeadingWhitespace($end);
$tokens->insertAt($end, new Token([T_WHITESPACE, "\n".$this->analyze($tokens)->getLineIndentation($index)]));
}
}

public function setWhitespacesConfig(WhitespacesFixerConfig $config)
{
if (!$this instanceof WhitespacesAwareFixerInterface) {
throw new \LogicException('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
}

$this->whitespacesConfig = $config;
}

private function getDefaultWhitespacesFixerConfig()
{
static $defaultWhitespacesFixerConfig = null;

if (null === $defaultWhitespacesFixerConfig) {
$defaultWhitespacesFixerConfig = new WhitespacesFixerConfig(' ', "\n");
}

return $defaultWhitespacesFixerConfig;
}

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

for ($i = $index + 1; $i < $tokens->count(); ++$i) {
if ($tokens[$i]->equals('(')) {
++$opening;
}

if ($tokens[$i]->equals(')')) {
if ($opening > 0) {
--$opening;
}

return $i;
}
}

return 0;
}

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

for ($i = $index + 1; $i < $tokens->count(); ++$i) {
if ($tokens[$i]->equals('[')) {
++$opening;
}

if ($tokens[$i]->equals(']')) {
if ($opening > 0) {
--$opening;
}

return $i;
}
}

return 0;
}

private function getNumberOfArguments(Tokens $tokens, $index)
{
if (T_FUNCTION !== $tokens[$index]->getId()) {
return 0;
}

$open = $tokens->getNextTokenOfKind($index, ['(']);

if ($tokens[$tokens->getNextMeaningfulToken($open)]->equals(')')) {
return 0;
}

$close = $this->analyze($tokens)->getClosingParenthesis($open);
$arguments = 1;

for ($i = $open + 1; $i < $close; ++$i) {
if ($tokens[$i]->equals('(')) {
$i = $this->analyze($tokens)->getClosingParenthesis($i);
}

if ($tokens[$i]->equals('[')) {
$i = $this->analyze($tokens)->getClosingBracket($i);
}

if ($tokens[$i]->equals(',')) {
++$arguments;
}
}

return $arguments;
}

protected function analyze(Tokens $tokens)
{
return new TokensAnalyzer($tokens);
}
}

App/CodingStandard/Utils/TokensAnalyzer.php

<?php

declare(strict_types=1);

namespace App\CodingStandard\Utils;

use Exception;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer as PhpCsFixerTokensAnalyzer;

final class TokensAnalyzer
{
/**
* @var Tokens
*/
private $tokens;

private $analyzer;

public function __construct(Tokens $tokens)
{
$this->tokens = $tokens;
$this->analyzer = new PhpCsFixerTokensAnalyzer($tokens);
}

public function __call($name, $arguments)
{
return \call_user_func_array([$this->analyzer, $name], $arguments);
}

/**
* @param int $index
*
* @return array
*
* @throws Exception
*/
public function getMethodArguments($index)
{
$methodName = $this->tokens->getNextMeaningfulToken($index);
$openParenthesis = $this->tokens->getNextMeaningfulToken($methodName);
$closeParenthesis = $this->getClosingParenthesis($openParenthesis);

$arguments = [];

for ($position = $openParenthesis + 1; $position < $closeParenthesis; ++$position) {
$token = $this->tokens[$position];

if ($token->isWhitespace()) {
continue;
}

$argumentType = null;
$argumentName = $position;
$argumentAsDefault = false;
$argumentNullable = false;

if (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent())) {
do {
if (false === $this->tokens[$argumentName]->isWhitespace()) {
$argumentType .= $this->tokens[$argumentName]->getContent();
}

++$argumentName;
} while (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent()));
}

$next = $this->tokens->getNextMeaningfulToken($argumentName);

if ($this->tokens[$next]->equals('=')) {
$argumentAsDefault = true;
$value = $this->tokens->getNextMeaningfulToken($next);
$argumentNullable = 'null' === $this->tokens[$value]->getContent();
}

$arguments[$position] = [
'type' => $argumentType,
'name' => $this->tokens[$argumentName]->getContent(),
'nullable' => $argumentNullable,
'asDefault' => $argumentAsDefault,
];

$nextComma = $this->getNextComma($position);

if (null === $nextComma) {
return $arguments;
}

$position = $nextComma;
}

return $arguments;
}

public function getArrayElements($index)
{
$openBracket = $index;
$closeBracket = $this->tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);

$elements = [];
file_put_contents('debug.log', 'open: '.$openBracket.' close: '.$closeBracket.PHP_EOL, FILE_APPEND);

for ($position = $openBracket + 1; $position < $closeBracket; ++$position) {
$token = $this->tokens[$position];

//file_put_contents('debug.log', $position. PHP_EOL, FILE_APPEND);

if ($token->isWhitespace()) {
continue;
}

$argumentType = null;
$argumentName = $position;
$argumentAsDefault = false;
$argumentNullable = false;

if (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent())) {
//file_put_contents('debug.log', 'preg match'. PHP_EOL, FILE_APPEND);

do {
if (false === $this->tokens[$argumentName]->isWhitespace()) {
$argumentType .= $this->tokens[$argumentName]->getContent();
}

++$argumentName;
} while (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent()));
}

$next = $this->tokens->getNextMeaningfulToken($argumentName);

//todo do we need?
if ($this->tokens[$next]->equals('=')) {
$argumentAsDefault = true;
$value = $this->tokens->getNextMeaningfulToken($next);
$argumentNullable = 'null' === $this->tokens[$value]->getContent();
}

$elements[$position] = [
'type' => $argumentType,
'name' => $this->tokens[$argumentName]->getContent(),
'nullable' => $argumentNullable,
'asDefault' => $argumentAsDefault,
];

$nextComma = $this->getNextComma($position); //this looks good

if (null === $nextComma) {
return $elements;
}

$position = $nextComma;
}

return $elements;
}

/**
* @param int $index
*
* @return int
*
* @throws Exception
*/
public function getNumberOfArguments($index)
{
return count($this->getMethodArguments($index));
}

public function getNumberOfElements($index)
{
return count($this->getArrayElements($index));
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function getNextComma($index)
{
do {
$index = $this->tokens->getNextMeaningfulToken($index);

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

switch (true) {
case $this->tokens[$index]->equals('('):
$index = $this->getClosingParenthesis($index);

break;
case $this->tokens[$index]->equals('['):
$index = $this->getClosingBracket($index);

break;
case $this->tokens[$index]->equals('{'):
$index = $this->getClosingCurlyBracket($index);

break;
case $this->tokens[$index]->equals(';'):
return;
}
} while (false === $this->tokens[$index]->equals(','));

return $index;
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function getNextSemiColon($index)
{
do {
$index = $this->tokens->getNextMeaningfulToken($index);

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

switch (true) {
case $this->tokens[$index]->equals('('):
$index = $this->getClosingParenthesis($index);

break;
case $this->tokens[$index]->equals('['):
$index = $this->getClosingBracket($index);

break;
case $this->tokens[$index]->equals('{'):
$index = $this->getClosingCurlyBracket($index);

break;
}
} while (false === $this->tokens[$index]->equals(';'));

return $index;
}

/**
* @param int $index
*
* @return array|string|null
*
* @throws Exception
*/
public function getReturnedType($index)
{
if (false === $this->tokens[$index]->isGivenKind(T_FUNCTION)) {
throw new Exception(sprintf('Expected token: T_FUNCTION Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

$methodName = $this->tokens->getNextMeaningfulToken($index);
$openParenthesis = $this->tokens->getNextMeaningfulToken($methodName);
$closeParenthesis = $this->getClosingParenthesis($openParenthesis);

$next = $this->tokens->getNextMeaningfulToken($closeParenthesis);

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

if (false === $this->tokens[$next]->isGivenKind(TokenSignatures::TYPINT_DOUBLE_DOTS)) {
return;
}

$next = $this->tokens->getNextMeaningfulToken($next);

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

$optionnal = $this->tokens[$next]->isGivenKind(TokenSignatures::TYPINT_OPTIONAL);

$next = $optionnal
? $this->tokens->getNextMeaningfulToken($next)
: $next;

do {
$return = $this->tokens[$next]->getContent();
++$next;

if ($this->tokens[$next]->isWhitespace() || $this->tokens[$next]->equals(';')) {
return $optionnal
? [$return, null]
: $return;
}
} while (false === $this->tokens[$index]->equals(['{', ';']));
}

/**
* @param int $index
*
* @return int|null
*/
public function getBeginningOfTheLine($index)
{
for ($i = $index; $i >= 0; --$i) {
if (false !== mb_strpos($this->tokens[$i]->getContent(), "\n")) {
return $i;
}
}
}

/**
* @param int $index
*
* @return int|null
*/
public function getEndOfTheLine($index)
{
for ($i = $index; $i < $this->tokens->count(); ++$i) {
if (false !== mb_strpos($this->tokens[$i]->getContent(), "\n")) {
return $i;
}
}
}

/**
* @param int $index
*
* @return int
*/
public function getSizeOfTheLine($index)
{
$start = $this->getBeginningOfTheLine($index);
$end = $this->getEndOfTheLine($index);
$size = 0;

$parts = explode("\n", $this->tokens[$start]->getContent());
$size += mb_strlen(end($parts));

$parts = explode("\n", $this->tokens[$end]->getContent());
$size += mb_strlen(current($parts));

for ($i = $start + 1; $i < $end; ++$i) {
$size += mb_strlen($this->tokens[$i]->getContent());
}

return $size;
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function endOfTheStatement($index)
{
do {
$index = $this->tokens->getNextMeaningfulToken($index);

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

switch (true) {
case $this->tokens[$index]->equals('('):
$index = $this->getClosingParenthesis($index);

break;
case $this->tokens[$index]->equals('['):
$index = $this->getClosingBracket($index);

break;
case $this->tokens[$index]->equals('{'):
$index = $this->getClosingCurlyBracket($index);

break;
}
} while (false === $this->tokens[$index]->equals('}'));

return $index;
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function getClosingParenthesis($index)
{
if (false === $this->tokens[$index]->equals('(')) {
throw new Exception(sprintf('Expected token: (. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('(')) {
$i = $this->getClosingParenthesis($i);

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

continue;
}

if ($this->tokens[$i]->equals(')')) {
return $i;
}
}
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function getClosingBracket($index)
{
if (false === $this->tokens[$index]->equals('[')) {
throw new Exception(sprintf('Expected token: [. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('[')) {
$i = $this->getClosingBracket($i);

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

continue;
}

if ($this->tokens[$i]->equals(']')) {
return $i;
}
}
}

/**
* @param int $index
*
* @return int|null
*
* @throws Exception
*/
public function getClosingCurlyBracket($index)
{
if (false === $this->tokens[$index]->equals('{')) {
throw new Exception(sprintf('Expected token: {. Token %d id contains %s.', $index, $this->tokens[$index]->getContent()));
}

for ($i = $index + 1; $i < $this->tokens->count(); ++$i) {
if ($this->tokens[$i]->equals('{')) {
$i = $this->getClosingCurlyBracket($i);

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

continue;
}

if ($this->tokens[$i]->equals('}')) {
return $i;
}
}
}

/**
* @param int $index
*
* @return bool
*
* @throws Exception
*/
public function isInsideSwitchCase($index)
{
$switch = null;
$ids = array_keys($this->tokens->toArray());

$switches = $this->findAllSequences([[[T_SWITCH]]]);
$intervals = [];

foreach ($switches as $i => $switch) {
$start = $this->tokens->getNextTokenOfKind($i, ['{']);
$end = $this->getClosingCurlyBracket($start);

$intervals[] = [$start, $end];
}

foreach ($intervals as $interval) {
[$start, $end] = $interval;

if ($index >= $start && $index <= $end) {
return true;
}
}

return false;
}

/**
* @param int $index
*
* @return string
*/
public function getLineIndentation($index)
{
$start = $this->getBeginningOfTheLine($index);
$token = $this->tokens[$start];
$parts = explode("\n", $token->getContent());

return end($parts);
}

/**
* @param mixed|null $start
* @param mixed|null $end
*
* @return array
*/
public function findAllSequences(array $seqs, $start = null, $end = null)
{
$sequences = [];

foreach ($seqs as $seq) {
$index = $start ?: 0;

do {
$extract = $this->tokens->findSequence($seq, (int) $index, $end);

if (null !== $extract) {
$keys = array_keys($extract);
$index = end($keys) + 1;
$sequences[reset($keys)] = $extract;
}
} while (null !== $extract);
}

ksort($sequences);

return $sequences;
}

/**
* @param int $startIndex
*
* @return array[]
*/
public function getElements($startIndex = null)
{
static $elementTokenKinds = [CT::T_USE_TRAIT, T_CONST, T_VARIABLE, T_FUNCTION];

if (null === $startIndex) {
foreach ($this->tokens as $startIndex => $token) {
if (!$token->isClassy()) {
continue;
}

$startIndex = $this->tokens->getNextTokenOfKind($startIndex, ['{']);

break;
}
}

++$startIndex;
$elements = [];

while (true) {
$element = [
'start' => $startIndex,
'visibility' => 'public',
'static' => false,
];

for ($i = $startIndex;; ++$i) {
$token = $this->tokens[$i];

if ($token->equals('}')) {
return $elements;
}

if ($token->isGivenKind(T_STATIC)) {
$element['static'] = true;

continue;
}

if ($token->isGivenKind([T_PROTECTED, T_PRIVATE])) {
$element['visibility'] = mb_strtolower($token->getContent());

continue;
}

if (!$token->isGivenKind([CT::T_USE_TRAIT, T_CONST, T_VARIABLE, T_FUNCTION])) {
continue;
}

$type = $this->detectElementType($this->tokens, $i);
$element['type'] = $type;

switch ($type) {
case 'method':
$element['methodName'] = $this->tokens[$this->tokens->getNextMeaningfulToken($i)]->getContent();

break;
case 'property':
$element['propertyName'] = $token->getContent();

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

break;
}

$elements[] = $element;
$startIndex = $element['end'] + 1;
}
}

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

if ($token->isGivenKind(CT::T_USE_TRAIT)) {
return 'use_trait';
}

if ($token->isGivenKind(T_CONST)) {
return 'constant';
}

if ($token->isGivenKind(T_VARIABLE)) {
return 'property';
}

$nameToken = $tokens[$tokens->getNextMeaningfulToken($index)];

if ($nameToken->equals([T_STRING, '__construct'], false)) {
return 'construct';
}

if ($nameToken->equals([T_STRING, '__destruct'], false)) {
return 'destruct';
}

if (
$nameToken->equalsAny([
[T_STRING, 'setUpBeforeClass'],
[T_STRING, 'tearDownAfterClass'],
[T_STRING, 'setUp'],
[T_STRING, 'tearDown'],
], false)
) {
return ['phpunit', mb_strtolower($nameToken->getContent())];
}

if ('__' === mb_substr($nameToken->getContent(), 0, 2)) {
return 'magic';
}

return 'method';
}

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

if ($tokens[$index]->equals('{')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
}

for (++$index; $tokens[$index]->isWhitespace(" \t") || $tokens[$index]->isComment(); ++$index);

--$index;

return $tokens[$index]->isWhitespace() ? $index - 1 : $index;
}
}

 

These were pulled (and maybe modified) from https://github.com/PedroTroller/PhpCSFixer-Custom-Fixers which I plan to base some customer fixers from.

0
Comment actions Permalink

Thank you! I created a project out of these files, configured PHP-CS-Fixer to use the .php_cs file as a custom coding standard, and it worked for me like a charm:
https://recordit.co/6697421YeM

Could you please record a similar screencast?

0
Comment actions Permalink

Thanks Eugene!

I created a scratch project and it is working for me, so the issue is on my end (in my other project, with my custom fixer) and not PHPStorm.

Thanks again, sorry to bother you.

0
Comment actions Permalink

and my issue on my custom fixer was a

file_put_contents

I was using for debugging  (via php-csfixer cli) that I forgot to comment out.

thanks for being my rubber duck!

0

Please sign in to leave a comment.