Skip to content

Commit

Permalink
Merge b9ab8ee into b116a0d
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Apr 29, 2023
2 parents b116a0d + b9ab8ee commit 51e0614
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
9 changes: 5 additions & 4 deletions PHPCSUtils/Utils/UseStatements.php
Expand Up @@ -199,6 +199,10 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
throw new RuntimeException('$stackPtr must be an import use statement');
}

if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) {
return Cache::get($phpcsFile, __METHOD__, $stackPtr);
}

$statements = [
'name' => [],
'function' => [],
Expand All @@ -208,13 +212,10 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
$endOfStatement = $phpcsFile->findNext([\T_SEMICOLON, \T_CLOSE_TAG], ($stackPtr + 1));
if ($endOfStatement === false) {
// Live coding or parse error.
Cache::set($phpcsFile, __METHOD__, $stackPtr, $statements);
return $statements;
}

if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) {
return Cache::get($phpcsFile, __METHOD__, $stackPtr);
}

++$endOfStatement;

$start = true;
Expand Down
32 changes: 32 additions & 0 deletions Tests/Utils/UseStatements/SplitImportUseStatementTest.php
Expand Up @@ -333,4 +333,36 @@ public function testResultIsCached()
$this->assertTrue($isCached, 'Cache::isCached() could not find the cached value');
$this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first');
}

/**
* Verify that the build-in caching is used when caching is enabled and a parse error is encountered.
*
* @return void
*/
public function testResultIsCachedForParseError()
{
$methodName = 'PHPCSUtils\\Utils\\UseStatements::splitImportUseStatement';
$cases = $this->dataSplitImportUseStatement();
$testMarker = $cases['parse-error']['testMarker'];
$expected = $cases['parse-error']['expected'];

$stackPtr = $this->getTargetToken($testMarker, \T_USE);

// Verify the caching works.
$origStatus = Cache::$enabled;
Cache::$enabled = true;

$resultFirstRun = UseStatements::splitImportUseStatement(self::$phpcsFile, $stackPtr);
$isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr);
$resultSecondRun = UseStatements::splitImportUseStatement(self::$phpcsFile, $stackPtr);

if ($origStatus === false) {
Cache::clear();
}
Cache::$enabled = $origStatus;

$this->assertSame($expected, $resultFirstRun, 'First result did not match expectation');
$this->assertTrue($isCached, 'Cache::isCached() could not find the cached value');
$this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first');
}
}

0 comments on commit 51e0614

Please sign in to comment.