Browse Source

Merge branch '2.2' into 2.3

# Conflicts:
#	src/Tokenizer/Token.php
Dariusz Ruminski 7 years ago
parent
commit
ae8f46b7f1
3 changed files with 41 additions and 17 deletions
  1. 16 1
      src/Tokenizer/Token.php
  2. 2 3
      src/Tokenizer/Tokens.php
  3. 23 13
      tests/Tokenizer/TokenTest.php

+ 16 - 1
src/Tokenizer/Token.php

@@ -59,9 +59,14 @@ class Token
             $this->isArray = true;
             $this->id = $token[0];
             $this->content = $token[1];
-        } else {
+        } elseif (is_string($token)) {
             $this->isArray = false;
             $this->content = $token;
+        } else {
+            throw new \InvalidArgumentException(sprintf(
+                'Cannot recognize input value as valid Token prototype, got "%s".',
+                is_object($token) ? get_class($token) : gettype($token)
+            ));
         }
     }
 
@@ -201,6 +206,10 @@ class Token
     }
 
     /**
+     * Get token's content.
+     *
+     * It shall be used only for getting the content of token, not for checking it against excepted value.
+     *
      * @return string
      */
     public function getContent()
@@ -211,6 +220,8 @@ class Token
     /**
      * Get token's id.
      *
+     * It shall be used only for getting the internal id of token, not for checking it against excepted value.
+     *
      * @return int|null
      */
     public function getId()
@@ -219,6 +230,10 @@ class Token
     }
 
     /**
+     * Get token's name.
+     *
+     * It shall be used only for getting the name of token, not for checking it against excepted value.
+     *
      * @return null|string token name
      */
     public function getName()

+ 2 - 3
src/Tokenizer/Tokens.php

@@ -24,6 +24,7 @@ use PhpCsFixer\Utils;
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  *
  * @method Token current()
+ * @method Token offsetGet($index)
  */
 class Tokens extends \SplFixedArray
 {
@@ -905,9 +906,7 @@ class Tokens extends \SplFixedArray
         // If we want to add less tokens than passed range contains then clear
         // not needed tokens.
         if ($itemsCount < $indexToChange) {
-            for ($i = $indexStart + $itemsCount; $i <= $indexEnd; ++$i) {
-                $this[$i]->clear();
-            }
+            $this->clearRange($indexStart + $itemsCount, $indexEnd);
         }
     }
 

+ 23 - 13
tests/Tokenizer/TokenTest.php

@@ -279,24 +279,34 @@ final class TokenTest extends TestCase
         ];
     }
 
-    public function testPropertiesOfArrayToken()
+    /**
+     * @param mixed       $prototype
+     * @param null|int    $expectedId
+     * @param null|string $expectedContent
+     * @param null|bool   $expectedIsArray
+     * @param null|string $expectedExceptionClass
+     *
+     * @dataProvider provideCreatingTokenCases
+     */
+    public function testCreatingToken($prototype, $expectedId, $expectedContent, $expectedIsArray, $expectedExceptionClass = null)
     {
-        $prototype = $this->getForeachTokenPrototype();
-        $token = $this->getForeachToken();
+        $this->setExpectedException($expectedExceptionClass);
 
-        $this->assertSame($prototype[0], $token->getId());
-        $this->assertSame($prototype[1], $token->getContent());
-        $this->assertTrue($token->isArray());
+        $token = new Token($prototype);
+        $this->assertSame($expectedId, $token->getId());
+        $this->assertSame($expectedContent, $token->getContent());
+        $this->assertSame($expectedIsArray, $token->isArray());
     }
 
-    public function testPropertiesOfNonArrayToken()
+    public function provideCreatingTokenCases()
     {
-        $prototype = $this->getBraceTokenPrototype();
-        $token = $this->getBraceToken();
-
-        $this->assertSame($prototype, $token->getContent());
-        $this->assertNull($token->getId());
-        $this->assertFalse($token->isArray());
+        return [
+            [[T_FOREACH, 'foreach'], T_FOREACH, 'foreach', true],
+            ['(', null, '(', false],
+            [123, null, null, null, 'InvalidArgumentException'],
+            [false, null, null, null, 'InvalidArgumentException'],
+            [null, null, null, null, 'InvalidArgumentException'],
+        ];
     }
 
     public function testEqualsDefaultIsCaseSensitive()