Skip to content

Commit

Permalink
Merge 4b1fec1 into 78940dd
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jan 26, 2018
2 parents 78940dd + 4b1fec1 commit a6e00ed
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
- brew update
- brew tap homebrew/homebrew-php
- brew install php71
- echo "memory_limit = 512M" > /opt/circleci/php/$(phpenv global)/etc/conf.d/memory.ini
- curl -sS https://getcomposer.org/installer | php
- php composer.phar global show hirak/prestissimo -q || php composer.phar global require --no-interaction --no-progress --optimize-autoloader hirak/prestissimo
override:
Expand Down
24 changes: 19 additions & 5 deletions src/Fixer/ControlStructure/YodaStyleFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ private function findComparisonEnd(Tokens $tokens, $index)
$count = count($tokens);
while ($index < $count) {
$token = $tokens[$index];
if ($token->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) {
++$index;

continue;
}

if ($this->isOfLowerPrecedence($token)) {
break;
}
Expand Down Expand Up @@ -175,20 +181,32 @@ private function findComparisonEnd(Tokens $tokens, $index)
private function findComparisonStart(Tokens $tokens, $index)
{
--$index;
$nonBlockFound = false;

while (0 <= $index) {
$token = $tokens[$index];
if ($token->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) {
--$index;

continue;
}

if ($this->isOfLowerPrecedence($token)) {
break;
}

$block = Tokens::detectBlockType($token);
if (null === $block) {
--$index;
$nonBlockFound = true;

continue;
}

if ($block['isStart']) {
if (
$block['isStart']
|| ($nonBlockFound && Tokens::BLOCK_TYPE_CURLY_BRACE === $block['type']) // closing of structure not related to the comparison
) {
break;
}

Expand Down Expand Up @@ -400,10 +418,6 @@ private function isListStatement(Tokens $tokens, $index, $end)
*/
private function isOfLowerPrecedence(Token $token)
{
if ($token->isGivenKind([T_WHITESPACE, T_COMMENT, T_DOC_COMMENT])) {
return false;
}

static $tokens;

if (null === $tokens) {
Expand Down
42 changes: 33 additions & 9 deletions tests/Fixer/ControlStructure/YodaStyleFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function provideFixCases()
['<?php $j = 2 * $myVar % 3 === $a;'],
['<?php return $k === 2 * $myVar % 3;'],
['<?php $l = $c > 2;'],
['<?php return $this->myObject->{$index}+$b === "";'],
['<?php return $this->myObject1->{$index}+$b === "";'],
['<?php return $m[2]+1 == 2;'],
// https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/693
['<?php return array(2) == $o;'],
Expand Down Expand Up @@ -267,16 +267,20 @@ public function provideFixCases()
'<?php return $this->myArray[$index]->a === "";',
],
[
'<?php return "" === $this->myObject-> {$index};',
'<?php return $this->myObject-> {$index} === "";',
'<?php return "" === $this->myObject2-> {$index};',
'<?php return $this->myObject2-> {$index} === "";',
],
[
'<?php return "" === $this->myObject->{$index}->a;',
'<?php return $this->myObject->{$index}->a === "";',
'<?php return "" === $this->myObject3->{$index}->a;',
'<?php return $this->myObject3->{$index}->a === "";',
],
[
'<?php return "" === $this->myObject->$index->a;',
'<?php return $this->myObject->$index->a === "";',
'<?php return "" === $this->myObject4->{$index}->{$index}->a;',
'<?php return $this->myObject4->{$index}->{$index}->a === "";',
],
[
'<?php return "" === $this->myObject4->$index->a;',
'<?php return $this->myObject4->$index->a === "";',
],
[
'<?php return self::MY_CONST === self::$myVariable;',
Expand Down Expand Up @@ -363,8 +367,28 @@ public function provideFixCases()
'<?php $i = $this/*a*//*b*//*c*//*d*//*e*//*f*/->getStuff() === 2;',
],
[
'<?php return "" === $this->myObject->{$index}->/*1*//*2*/b;',
'<?php return $this->myObject->{$index}->/*1*//*2*/b === "";',
'<?php return "" === $this->myObject5->{$index}->/*1*//*2*/b;',
'<?php return $this->myObject5->{$index}->/*1*//*2*/b === "";',
],
[
'<?php
function hello() {}
1 === $a ? b() : c();
',
'<?php
function hello() {}
$a === 1 ? b() : c();
',
],
[
'<?php
class A{}
1 === $a ? b() : c();
',
'<?php
class A{}
$a === 1 ? b() : c();
',
],
];
}
Expand Down

0 comments on commit a6e00ed

Please sign in to comment.