Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from supercid/hotfix/support-traits
Browse files Browse the repository at this point in the history
Add Traits Support
  • Loading branch information
Hannu Pölönen committed Mar 11, 2019
2 parents 958ba3f + c459d15 commit 7bc0710
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Pearify/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class File
public $tokens;
public $originalUse;
private $originalNamespace;
private $lastClassKey;

public function __construct($src)
{
Expand Down
59 changes: 54 additions & 5 deletions src/Pearify/Utils/ClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ class ClassFinder
const IGNORED_KEYWORDS = ['parent', 'self', 'static'];
private $file;

/**
* ClassFinder constructor.
*
* @param File $file
*/
public function __construct(File $file)
{
$this->file = $file;
}

/**
* Generator function that returns a FoundClass if found
*
* @return \Generator
*/
public function find()
{
foreach ($this->file->tokens as $i => $t) {
Expand All @@ -45,16 +55,21 @@ public function find()
yield $c;
}
$j = $c->to + 1;
while ($this->file->tokens[$j] == ",") {
// +2 to consume comma and whitepace
while ($this->file->tokens[$j] === ',') {
// +2 to consume comma and whitespace
$c = self::findClassInNextTokens($this->file->tokens, $j + 2);
if ($c) {
yield $c;
$j = $c->to;
}
$j++;
}

} elseif (TokenUtils::isTokenType($t, array(T_USE))) {
$c = self::findClassInNextTokens($this->file->tokens, $i + 2);
// Check if use statement is after class opening bracket
if ($c && $this->isAfterOpeningBracket($c)) {
yield $c;
}
} elseif (TokenUtils::isTokenType($t, array(T_PAAMAYIM_NEKUDOTAYIM, T_DOUBLE_COLON))) {
$c = self::findClassInPreviousTokens($this->file->tokens, $i - 1);
if ($c) {
Expand Down Expand Up @@ -84,7 +99,7 @@ public function find()

while ($this->file->tokens[$j] != ")") {
if ($this->file->tokens[$j] == ',') {
// +2 to consume comma and whitepace
// +2 to consume comma and whitespace
$c = self::findClassInNextTokens($this->file->tokens, $j + 2);
if ($c) {
yield $c;
Expand All @@ -98,6 +113,32 @@ public function find()
}
}

/**
* Returns if given token is after a opening bracket
*
* @param FoundClass $c
* @return bool
*/
private function isAfterOpeningBracket(FoundClass $c)
{
for ($k = $c->from; $k >= 0; $k--) {
if (is_string($this->file->tokens[$k])
&& $this->file->tokens[$k] === '{'
) {
return true;
}
}
return false;
}

/**
* Given an index $i, walk through remaining tokens and returns
* a FoundClass if found
*
* @param $tokens
* @param int $i
* @return FoundClass|null
*/
public static function findClassInNextTokens($tokens, $i)
{
$classname = '';
Expand All @@ -112,6 +153,14 @@ public static function findClassInNextTokens($tokens, $i)
return null;
}

/**
* Given an index $i, walk backwards through tokens and returns
* a FoundClass if found
*
* @param $tokens
* @param $i
* @return FoundClass|null
*/
public static function findClassInPreviousTokens($tokens, $i)
{
$classname = '';
Expand All @@ -136,4 +185,4 @@ private static function tokenStr($token)
{
return is_string($token) ? $token : $token[1];
}
}
}
10 changes: 3 additions & 7 deletions src/Pearify/Utils/TokenUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public static function positionForSequence(array $sequence, $tokens, $startFrom
$seqIterator->next();
}
}

return;
}

/**
Expand All @@ -98,7 +96,7 @@ public static function sequenceMatch(array $tokenSequence, $tokens, $startFrom =
{
$seqIterator = (new ArrayObject($tokenSequence))->getIterator();
$tokenIterator = (new ArrayObject($tokens))->getIterator();
if ($startFrom != null) {
if ($startFrom !== null) {
$tokenIterator->seek($startFrom);
}
while ($tokenIterator->valid()) {
Expand Down Expand Up @@ -133,8 +131,6 @@ public static function sequenceMatch(array $tokenSequence, $tokens, $startFrom =
$seqIterator->next();
}
}

return;
}

/**
Expand All @@ -157,7 +153,7 @@ private static function seekToNextType(ArrayIterator $tokenIterator, $type)
* Checks whether a given token is any one of the specified token types
*
* @param array $token the token to check
* @param array $types the array of types
* @param array|int $types the array of types
* @return bool true if the token matches one the types
*/
public static function isTokenType($token, $types)
Expand All @@ -167,4 +163,4 @@ public static function isTokenType($token, $types)
}
return is_array($token) ? in_array($token[0], $types) : in_array($token, $types);
}
}
}

0 comments on commit 7bc0710

Please sign in to comment.