Browse Source

minor #6215 DX: Doctrine\Annotation\Tokens - fix phpstan violations (keradus)

This PR was squashed before being merged into the master branch.

Discussion
----------

DX: Doctrine\Annotation\Tokens - fix phpstan violations

Commits
-------

55798598e DX: Doctrine\Annotation\Tokens - fix phpstan violations
Dariusz Ruminski 3 years ago
parent
commit
efeab76099
1 changed files with 26 additions and 6 deletions
  1. 26 6
      src/Doctrine/Annotation/Tokens.php

+ 26 - 6
src/Doctrine/Annotation/Tokens.php

@@ -36,7 +36,7 @@ final class Tokens extends \SplFixedArray
             throw new \InvalidArgumentException('Input must be a T_DOC_COMMENT token.');
         }
 
-        $tokens = new self();
+        $tokens = [];
 
         $content = $input->getContent();
         $ignoredTextPosition = 0;
@@ -127,6 +127,31 @@ final class Tokens extends \SplFixedArray
             $tokens[] = new Token(DocLexer::T_NONE, substr($content, $ignoredTextPosition));
         }
 
+        return self::fromArray($tokens);
+    }
+
+    /**
+     * Create token collection from array.
+     *
+     * @param Token[] $array       the array to import
+     * @param ?bool   $saveIndices save the numeric indices used in the original array, default is yes
+     */
+    public static function fromArray($array, $saveIndices = null): self
+    {
+        $tokens = new self(\count($array));
+
+        if (null === $saveIndices || $saveIndices) {
+            foreach ($array as $key => $val) {
+                $tokens[$key] = $val;
+            }
+        } else {
+            $index = 0;
+
+            foreach ($array as $val) {
+                $tokens[$index++] = $val;
+            }
+        }
+
         return $tokens;
     }
 
@@ -273,11 +298,6 @@ final class Tokens extends \SplFixedArray
             ));
         }
 
-        if (null === $index) {
-            $index = \count($this);
-            $this->setSize($this->getSize() + 1);
-        }
-
         parent::offsetSet($index, $token);
     }