-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to move scope management to ScopeManager class (#265)
* Fix some minor type errors and improve code comments * Remove unnecessary scopeEndIndexCache * Add more function comments * If no scope closer is found, use the end of the file * Add ScopeManager * Improve index of ScopeManager using scopeStart * Replace scope list with ScopeManager * Move getScopesClosedBy to ScopeManager * Linting fixes * Support php 5.5 by not passing expression to empty) * Remove unused imports
- Loading branch information
1 parent
e1b89d1
commit 2747116
Showing
2 changed files
with
130 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace VariableAnalysis\Lib; | ||
|
||
use VariableAnalysis\Lib\ScopeInfo; | ||
use VariableAnalysis\Lib\Helpers; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class ScopeManager | ||
{ | ||
/** | ||
* An associative array of a list of token index pairs which start and end | ||
* scopes and will be used to check for unused variables. | ||
* | ||
* The outer array of scopes is keyed by a string containing the filename. | ||
* The inner array of scopes in keyed by the scope start token index. | ||
* | ||
* @var array<string, array<int, ScopeInfo>> | ||
*/ | ||
private $scopes = []; | ||
|
||
/** | ||
* Add a scope's start and end index to our record for the file. | ||
* | ||
* @param File $phpcsFile | ||
* @param int $scopeStartIndex | ||
* | ||
* @return ScopeInfo | ||
*/ | ||
public function recordScopeStartAndEnd(File $phpcsFile, $scopeStartIndex) | ||
{ | ||
$scopeEndIndex = Helpers::getScopeCloseForScopeOpen($phpcsFile, $scopeStartIndex); | ||
$filename = $phpcsFile->getFilename(); | ||
if (! isset($this->scopes[$filename])) { | ||
$this->scopes[$filename] = []; | ||
} | ||
Helpers::debug('recording scope for file', $filename, 'start/end', $scopeStartIndex, $scopeEndIndex); | ||
$scope = new ScopeInfo($scopeStartIndex, $scopeEndIndex); | ||
$this->scopes[$filename][$scopeStartIndex] = $scope; | ||
return $scope; | ||
} | ||
|
||
/** | ||
* Return the scopes for a file. | ||
* | ||
* @param string $filename | ||
* | ||
* @return ScopeInfo[] | ||
*/ | ||
public function getScopesForFilename($filename) | ||
{ | ||
if (empty($this->scopes[$filename])) { | ||
return []; | ||
} | ||
return array_values($this->scopes[$filename]); | ||
} | ||
|
||
/** | ||
* Return the scope for a scope start index. | ||
* | ||
* @param string $filename | ||
* @param int $scopeStartIndex | ||
* | ||
* @return ScopeInfo|null | ||
*/ | ||
public function getScopeForScopeStart($filename, $scopeStartIndex) | ||
{ | ||
if (empty($this->scopes[$filename][$scopeStartIndex])) { | ||
return null; | ||
} | ||
return $this->scopes[$filename][$scopeStartIndex]; | ||
} | ||
|
||
/** | ||
* Find scopes closed by a scope close index. | ||
* | ||
* @param string $filename | ||
* @param int $scopeEndIndex | ||
* | ||
* @return ScopeInfo[] | ||
*/ | ||
public function getScopesForScopeEnd($filename, $scopeEndIndex) | ||
{ | ||
$scopePairsForFile = $this->getScopesForFilename($filename); | ||
$scopeIndicesThisCloses = array_reduce( | ||
$scopePairsForFile, | ||
/** | ||
* @param ScopeInfo[] $found | ||
* @param ScopeInfo $scope | ||
* | ||
* @return ScopeInfo[] | ||
*/ | ||
function ($found, $scope) use ($scopeEndIndex) { | ||
if (! is_int($scope->scopeEndIndex)) { | ||
Helpers::debug('No scope closer found for scope start', $scope->scopeStartIndex); | ||
return $found; | ||
} | ||
|
||
if ($scopeEndIndex === $scope->scopeEndIndex) { | ||
$found[] = $scope; | ||
} | ||
return $found; | ||
}, | ||
[] | ||
); | ||
return $scopeIndicesThisCloses; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters