Skip to content

Commit

Permalink
minor PHP-CS-Fixer#538 safer curly braces checking (keradus)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.0.x-dev branch (closes PHP-CS-Fixer#538).

Discussion
----------

safer curly braces checking

- Safer check if token is { or }
- little speed impr. in `Tokens::getNamespaceUseIndexes`

Commits
-------

062b933 safer curly braces checking
  • Loading branch information
keradus committed Sep 10, 2014
2 parents 6864c62 + 062b933 commit 625dd27
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/PSR2/BracesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private function fixMissingControlBraces(Tokens $tokens)
// do not add braces for cases:
// - structure without block, e.g. while ($iter->next());
// - structure with block, e.g. while ($i) {...}, while ($i) : {...} endwhile;
if (in_array($tokenAfterParenthesis->content, array(';', '{', ':'), true)) {
if ($tokenAfterParenthesis->equalsAny(array(';', '{', ':'))) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/PSR2/FunctionDeclarationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function fix(\SplFileInfo $file, $content)
$startBraceIndex = null;
$startBraceToken = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'), $startBraceIndex);

if ('{' === $startBraceToken->content) {
if ($startBraceToken->equals('{')) {
// fix single-line whitespace before {
// eg: `function foo(){}` => `function foo() {}`
// eg: `function foo() {}` => `function foo() {}`
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/ReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function fix(\SplFileInfo $file, $content)

$prevNonWhitespaceToken = $tokens->getPrevNonWhitespace($index);

if (!in_array($prevNonWhitespaceToken->content, array(';', '}'), true)) {
if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) {
continue;
}

Expand Down
22 changes: 12 additions & 10 deletions Symfony/CS/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,12 @@ public function getClassyElements()
continue;
}

if ('{' === $token->content || $token->isGivenKind(array(T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES))) {
if ($token->equals('{') || $token->isGivenKind(array(T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES))) {
++$curlyBracesLevel;
continue;
}

if ('}' === $token->content) {
if ($token->equals('}')) {
--$curlyBracesLevel;

if (0 === $curlyBracesLevel) {
Expand Down Expand Up @@ -498,14 +498,16 @@ public function getNamespaceUseIndexes($perNamespace = false)
$bracesLevel = 0;
$namespaceIndex = 0;

$namespaceWithBraces = false;
//foreach ($this as $index => $token) {
for ($index = 0, $limit = $this->count(); $index < $limit; ++$index) {
$token = $this[$index];

foreach ($this as $index => $token) {
if (T_NAMESPACE === $token->id) {
$nextToken = $this->getNextTokenOfKind($index, array(';', '{'));
$nextTokenIndex = null;
$nextToken = $this->getNextTokenOfKind($index, array(';', '{'), $nextTokenIndex);

if ('{' === $nextToken->content) {
$namespaceWithBraces = true;
if ($nextToken->equals('{')) {
$index = $nextTokenIndex;
}

if ($perNamespace) {
Expand All @@ -515,17 +517,17 @@ public function getNamespaceUseIndexes($perNamespace = false)
continue;
}

if ('{' === $token->content) {
if ($token->equals('{')) {
++$bracesLevel;
continue;
}

if ('}' === $token->content) {
if ($token->equals('}')) {
--$bracesLevel;
continue;
}

if (T_USE !== $token->id || $bracesLevel > ($namespaceWithBraces ? 1 : 0)) {
if (T_USE !== $token->id || 0 < $bracesLevel) {
continue;
}

Expand Down

0 comments on commit 625dd27

Please sign in to comment.