From db99ef949bc5313989f9257f488df4b822cd5a8a Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 23 Nov 2023 11:23:53 -0800 Subject: [PATCH] Calculate original unprefixed namespaces when running repeatedly --- src/ChangeEnumerator.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ChangeEnumerator.php b/src/ChangeEnumerator.php index ce7fc904..67bdeda3 100644 --- a/src/ChangeEnumerator.php +++ b/src/ChangeEnumerator.php @@ -62,13 +62,16 @@ public function __construct(StraussConfig $config) */ public function getDiscoveredNamespaces(?string $namespacePrefix = ''): array { - $discoveredNamespaceReplacements = array_filter( - $this->discoveredNamespaces, - function (string $replacement) use ($namespacePrefix) { - return empty($namespacePrefix) || ! str_starts_with($replacement, $namespacePrefix); - }, - ARRAY_FILTER_USE_KEY - ); + $discoveredNamespaceReplacements = []; + + // When running subsequent times, try to discover the original namespaces. + // This is naive: it will not work where namespace replacement patterns have been used. + foreach ($this->discoveredNamespaces as $key => $value) { + $unprefixed = str_starts_with($this->namespacePrefix, $key) + ? ltrim(substr($key, strlen($this->namespacePrefix)), '\\') + : $key; + $discoveredNamespaceReplacements[ $unprefixed ] = $value; + } uksort($discoveredNamespaceReplacements, function ($a, $b) { return strlen($a) <=> strlen($b);