Skip to content

Commit c621db8

Browse files
authored
Merge pull request #467 from PHPCSStandards/utils/usestatements-splitimportuse-cache-earlier
UseStatements::splitImportUseStatement(): cache more often and check cache earlier
2 parents fa8932a + b9ab8ee commit c621db8

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

PHPCSUtils/Utils/UseStatements.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
199199
throw new RuntimeException('$stackPtr must be an import use statement');
200200
}
201201

202+
if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) {
203+
return Cache::get($phpcsFile, __METHOD__, $stackPtr);
204+
}
205+
202206
$statements = [
203207
'name' => [],
204208
'function' => [],
@@ -208,13 +212,10 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
208212
$endOfStatement = $phpcsFile->findNext([\T_SEMICOLON, \T_CLOSE_TAG], ($stackPtr + 1));
209213
if ($endOfStatement === false) {
210214
// Live coding or parse error.
215+
Cache::set($phpcsFile, __METHOD__, $stackPtr, $statements);
211216
return $statements;
212217
}
213218

214-
if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) {
215-
return Cache::get($phpcsFile, __METHOD__, $stackPtr);
216-
}
217-
218219
++$endOfStatement;
219220

220221
$start = true;

Tests/Utils/UseStatements/SplitImportUseStatementTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,36 @@ public function testResultIsCached()
333333
$this->assertTrue($isCached, 'Cache::isCached() could not find the cached value');
334334
$this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first');
335335
}
336+
337+
/**
338+
* Verify that the build-in caching is used when caching is enabled and a parse error is encountered.
339+
*
340+
* @return void
341+
*/
342+
public function testResultIsCachedForParseError()
343+
{
344+
$methodName = 'PHPCSUtils\\Utils\\UseStatements::splitImportUseStatement';
345+
$cases = $this->dataSplitImportUseStatement();
346+
$testMarker = $cases['parse-error']['testMarker'];
347+
$expected = $cases['parse-error']['expected'];
348+
349+
$stackPtr = $this->getTargetToken($testMarker, \T_USE);
350+
351+
// Verify the caching works.
352+
$origStatus = Cache::$enabled;
353+
Cache::$enabled = true;
354+
355+
$resultFirstRun = UseStatements::splitImportUseStatement(self::$phpcsFile, $stackPtr);
356+
$isCached = Cache::isCached(self::$phpcsFile, $methodName, $stackPtr);
357+
$resultSecondRun = UseStatements::splitImportUseStatement(self::$phpcsFile, $stackPtr);
358+
359+
if ($origStatus === false) {
360+
Cache::clear();
361+
}
362+
Cache::$enabled = $origStatus;
363+
364+
$this->assertSame($expected, $resultFirstRun, 'First result did not match expectation');
365+
$this->assertTrue($isCached, 'Cache::isCached() could not find the cached value');
366+
$this->assertSame($resultFirstRun, $resultSecondRun, 'Second result did not match first');
367+
}
336368
}

0 commit comments

Comments
 (0)