Skip to content
Permalink
Browse files

OperatorLinebreakFixer - discovering ternary operator (#76)

  • Loading branch information...
kubawerlos committed Oct 12, 2019
1 parent ceebae8 commit 6ff9dc194ea1c9e7e840fb40015f108ccc6fe33a
@@ -8,7 +8,7 @@

[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
![Tests](https://img.shields.io/badge/tests-1134-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1153-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)

@@ -27,6 +27,11 @@
<PossiblyNullArrayAccess errorLevel='suppress' />
<PossiblyNullArrayOffset errorLevel='suppress' />
<PossiblyNullOperand errorLevel='suppress' />
<PossiblyUnusedMethod>
<errorLevel type='suppress'>
<file name='./src/Analyzer/Analysis/SwitchAnalysis.php' />
</errorLevel>
</PossiblyUnusedMethod>
<UnusedClass errorLevel='suppress' />
</issueHandlers>
</psalm>
@@ -0,0 +1,24 @@
<?php
declare(strict_types = 1);
namespace PhpCsFixerCustomFixers\Analyzer\Analysis;
/**
* @internal
*/
final class CaseAnalysis
{
/** @var int */
private $colonIndex;
public function __construct(int $colonIndex)
{
$this->colonIndex = $colonIndex;
}
public function getColonIndex(): int
{
return $this->colonIndex;
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types = 1);
namespace PhpCsFixerCustomFixers\Analyzer\Analysis;
/**
* @internal
*/
final class SwitchAnalysis
{
/** @var int */
private $curlyBracesStart;
/** @var int */
private $curlyBracesEnd;
/** @var CaseAnalysis[] */
private $cases = [];
/**
* @param CaseAnalysis[] $cases
*/
public function __construct(int $curlyBracesStart, int $curlyBracesEnd, array $cases)
{
$this->curlyBracesStart = $curlyBracesStart;
$this->curlyBracesEnd = $curlyBracesEnd;
$this->cases = $cases;
}
public function getCurlyBracesStart(): int
{
return $this->curlyBracesStart;
}
public function getCurlyBracesEnd(): int
{
return $this->curlyBracesEnd;
}
/**
* @return CaseAnalysis[]
*/
public function getCases(): array
{
return $this->cases;
}
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types = 1);
namespace PhpCsFixerCustomFixers\Analyzer;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixerCustomFixers\Analyzer\Analysis\CaseAnalysis;
use PhpCsFixerCustomFixers\Analyzer\Analysis\SwitchAnalysis;
/**
* @internal
*/
final class SwitchAnalyzer
{
public function getSwitchAnalysis(Tokens $tokens, int $switchIndex): SwitchAnalysis
{
if (!$tokens[$switchIndex]->isGivenKind(T_SWITCH)) {
throw new \InvalidArgumentException(\sprintf('Index %d is not "switch".', $switchIndex));
}
/** @var int $indexParenthesisStart */
$indexParenthesisStart = $tokens->getNextMeaningfulToken($switchIndex);
$indexParenthesisEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $indexParenthesisStart);
/** @var int $indexCurlyBracesStart */
$indexCurlyBracesStart = $tokens->getNextMeaningfulToken($indexParenthesisEnd);
$indexCurlyBracesEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $indexCurlyBracesStart);
$cases = [];
$ternaryOperatorDepth = 0;
$index = $indexCurlyBracesStart;
while ($index < $indexCurlyBracesEnd) {
$index++;
if ($tokens[$index]->isGivenKind(T_SWITCH)) {
$index = (new self())->getSwitchAnalysis($tokens, $index)->getCurlyBracesEnd();
continue;
}
if ($tokens[$index]->equals('?')) {
$ternaryOperatorDepth++;
continue;
}
if (!$tokens[$index]->equals(':')) {
continue;
}
if ($ternaryOperatorDepth > 0) {
$ternaryOperatorDepth--;
continue;
}
$cases[] = new CaseAnalysis($index);
}
return new SwitchAnalysis($indexCurlyBracesStart, $indexCurlyBracesEnd, $cases);
}
}

0 comments on commit 6ff9dc1

Please sign in to comment.
You can’t perform that action at this time.