Skip to content

Commit a9e6ce5

Browse files
authored
Merge pull request #196 from PHPCSStandards/feature/usestatements-split-off-merging-to-separate-method
UseStatements: new `mergeImportUseStatements()` method
2 parents 87ca29d + 7264b96 commit a9e6ce5

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

PHPCSUtils/Utils/UseStatements.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
376376
*
377377
* @see \PHPCSUtils\AbstractSniffs\AbstractFileContextSniff
378378
* @see \PHPCSUtils\Utils\UseStatements::splitImportUseStatement()
379+
* @see \PHPCSUtils\Utils\UseStatements::mergeImportUseStatements()
379380
*
380381
* @since 1.0.0-alpha3
381382
*
@@ -395,27 +396,59 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
395396
public static function splitAndMergeImportUseStatement(File $phpcsFile, $stackPtr, $previousUseStatements)
396397
{
397398
try {
398-
$useStatements = self::splitImportUseStatement($phpcsFile, $stackPtr);
399-
400-
if (isset($previousUseStatements['name']) === false) {
401-
$previousUseStatements['name'] = $useStatements['name'];
402-
} else {
403-
$previousUseStatements['name'] += $useStatements['name'];
404-
}
405-
if (isset($previousUseStatements['function']) === false) {
406-
$previousUseStatements['function'] = $useStatements['function'];
407-
} else {
408-
$previousUseStatements['function'] += $useStatements['function'];
409-
}
410-
if (isset($previousUseStatements['const']) === false) {
411-
$previousUseStatements['const'] = $useStatements['const'];
412-
} else {
413-
$previousUseStatements['const'] += $useStatements['const'];
414-
}
399+
$useStatements = self::splitImportUseStatement($phpcsFile, $stackPtr);
400+
$previousUseStatements = self::mergeImportUseStatements($previousUseStatements, $useStatements);
415401
} catch (RuntimeException $e) {
416402
// Not an import use statement.
417403
}
418404

419405
return $previousUseStatements;
420406
}
407+
408+
/**
409+
* Merge two import use statement arrays.
410+
*
411+
* Beware: this method should only be used to combine the import use statements found in *one* file.
412+
* Do NOT combine the statements of multiple files as the result will be inaccurate and unreliable.
413+
*
414+
* @see \PHPCSUtils\Utils\UseStatements::splitImportUseStatement()
415+
*
416+
* @since 1.0.0-alpha4
417+
*
418+
* @param array $previousUseStatements The import `use` statements collected so far.
419+
* This should be either the output of a
420+
* previous call to this method or the output of
421+
* an earlier call to the
422+
* {@see UseStatements::splitImportUseStatement()}
423+
* method.
424+
* @param array $currentUseStatement The parsed import `use` statements to merge with
425+
* the previously collected use statements.
426+
* This should be the output of a call to the
427+
* {@see UseStatements::splitImportUseStatement()}
428+
* method.
429+
*
430+
* @return array A multi-level array containing information about the current `use` statement combined with
431+
* the previously collected `use` statement information.
432+
* See {@see UseStatements::splitImportUseStatement()} for more details about the array format.
433+
*/
434+
public static function mergeImportUseStatements($previousUseStatements, $currentUseStatement)
435+
{
436+
if (isset($previousUseStatements['name']) === false) {
437+
$previousUseStatements['name'] = $currentUseStatement['name'];
438+
} else {
439+
$previousUseStatements['name'] += $currentUseStatement['name'];
440+
}
441+
if (isset($previousUseStatements['function']) === false) {
442+
$previousUseStatements['function'] = $currentUseStatement['function'];
443+
} else {
444+
$previousUseStatements['function'] += $currentUseStatement['function'];
445+
}
446+
if (isset($previousUseStatements['const']) === false) {
447+
$previousUseStatements['const'] = $currentUseStatement['const'];
448+
} else {
449+
$previousUseStatements['const'] += $currentUseStatement['const'];
450+
}
451+
452+
return $previousUseStatements;
453+
}
421454
}

Tests/Utils/UseStatements/SplitAndMergeImportUseStatementTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use PHPCSUtils\Utils\UseStatements;
1515

1616
/**
17-
* Tests for the \PHPCSUtils\Utils\UseStatements::splitAndMergeImportUseStatement() method.
17+
* Tests for the \PHPCSUtils\Utils\UseStatements::splitAndMergeImportUseStatement()
18+
* and the \PHPCSUtils\Utils\UseStatements::mergeImportUseStatements() method.
1819
*
1920
* @covers \PHPCSUtils\Utils\UseStatements::splitAndMergeImportUseStatement
21+
* @covers \PHPCSUtils\Utils\UseStatements::mergeImportUseStatements
2022
*
2123
* @group usestatements
2224
*

0 commit comments

Comments
 (0)