Skip to content

Commit aee8bef

Browse files
committed
Merge branch 'feature/yield-php5.4' of https://github.com/webimpress/PHP_CodeSniffer
2 parents 1334f59 + 784c564 commit aee8bef

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function getErrorList()
9595
548 => 1,
9696
641 => 1,
9797
669 => 1,
98+
688 => 1,
9899
744 => 1,
99100
748 => 1,
100101
767 => 1,
@@ -112,15 +113,6 @@ public function getErrorList()
112113
890 => 1,
113114
);
114115

115-
// The yield tests will only work in PHP versions where yield exists and
116-
// will throw errors in earlier versions.
117-
if (PHP_VERSION_ID < 50500) {
118-
$errors[676] = 1;
119-
$errors[874] = 1;
120-
} else {
121-
$errors[688] = 1;
122-
}
123-
124116
// Scalar type hints only work from PHP 7 onwards.
125117
if (PHP_VERSION_ID >= 70000) {
126118
$errors[17] = 3;

src/Tokenizers/PHP.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,72 @@ protected function tokenize($string)
731731
continue;
732732
}//end if
733733

734+
/*
735+
Before PHP 7.0, the "yield from" was tokenized as
736+
T_YIELD, T_WHITESPACE and T_STRING. So look for
737+
and change this token in earlier versions.
738+
*/
739+
740+
if (PHP_VERSION_ID < 70000
741+
&& PHP_VERSION_ID >= 50500
742+
&& $tokenIsArray === true
743+
&& $token[0] === T_YIELD
744+
&& isset($tokens[($stackPtr + 1)]) === true
745+
&& isset($tokens[($stackPtr + 2)]) === true
746+
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
747+
&& $tokens[($stackPtr + 2)][0] === T_STRING
748+
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
749+
) {
750+
$newToken = array();
751+
$newToken['code'] = T_YIELD_FROM;
752+
$newToken['type'] = 'T_YIELD_FROM';
753+
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
754+
$finalTokens[$newStackPtr] = $newToken;
755+
756+
$newStackPtr++;
757+
$stackPtr += 2;
758+
continue;
759+
}
760+
761+
/*
762+
Before PHP 5.5, the yield keyword was tokenized as
763+
T_STRING. So look for and change this token in
764+
earlier versions.
765+
Checks also if it is just "yield" or "yield from".
766+
*/
767+
768+
if (PHP_VERSION_ID < 50500
769+
&& $tokenIsArray === true
770+
&& $token[0] === T_STRING
771+
&& strtolower($token[1]) === 'yield'
772+
) {
773+
if (isset($tokens[($stackPtr + 1)]) === true
774+
&& isset($tokens[($stackPtr + 2)]) === true
775+
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
776+
&& $tokens[($stackPtr + 2)][0] === T_STRING
777+
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
778+
) {
779+
$newToken = array();
780+
$newToken['code'] = T_YIELD_FROM;
781+
$newToken['type'] = 'T_YIELD_FROM';
782+
$newToken['content'] = $token[1].$tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
783+
$finalTokens[$newStackPtr] = $newToken;
784+
785+
$newStackPtr++;
786+
$stackPtr += 2;
787+
continue;
788+
}
789+
790+
$newToken = array();
791+
$newToken['code'] = T_YIELD;
792+
$newToken['type'] = 'T_YIELD';
793+
$newToken['content'] = $token[1];
794+
$finalTokens[$newStackPtr] = $newToken;
795+
796+
$newStackPtr++;
797+
continue;
798+
}//end if
799+
734800
/*
735801
Before PHP 5.6, the ... operator was tokenized as three
736802
T_STRING_CONCAT tokens in a row. So look for and combine

0 commit comments

Comments
 (0)