diff --git a/src/ChangeEnumerator.php b/src/ChangeEnumerator.php index 0f468b3e..b3c4c555 100644 --- a/src/ChangeEnumerator.php +++ b/src/ChangeEnumerator.php @@ -207,11 +207,8 @@ public function find(string $contents): string function ($matches) { // If we're inside a namespace other than the global namespace: - if (1 === preg_match('/\s*namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*/', $matches[0])) { - - if(isset($matches[1])) { - $this->addDiscoveredNamespaceChange($matches[1]); - } + if (1 === preg_match('/^\s*namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*/', $matches[0])) { + $this->addDiscoveredNamespaceChange($matches[1]); return $matches[0]; } diff --git a/tests/Issues/StraussIssue91Test.php b/tests/Issues/StraussIssue91Test.php new file mode 100644 index 00000000..87a96080 --- /dev/null +++ b/tests/Issues/StraussIssue91Test.php @@ -0,0 +1,52 @@ +testsWorkingDir); + + file_put_contents($this->testsWorkingDir . '/composer.json', $composerJsonString); + + exec('composer install'); + + $inputInterfaceMock = $this->createMock(InputInterface::class); + $outputInterfaceMock = $this->createMock(OutputInterface::class); + + $strauss = new Compose(); + + $result = $strauss->run($inputInterfaceMock, $outputInterfaceMock); + + self::assertEquals(0, $result); + } +} diff --git a/tests/Unit/ChangeEnumeratorTest.php b/tests/Unit/ChangeEnumeratorTest.php index cfbfba52..c640d66e 100644 --- a/tests/Unit/ChangeEnumeratorTest.php +++ b/tests/Unit/ChangeEnumeratorTest.php @@ -120,6 +120,50 @@ class MyOtherClass { self::assertNotContains('MyOtherClass', $sut->getDiscoveredClasses()); } + public function testItDoesNotFindNamespaceInComment(): void + { + + $validPhp = <<<'EOD' +createMock(StraussConfig::class); + $sut = new ChangeEnumerator($config); + + try { + $sut->find($validPhp); + } catch (\PHPUnit\Framework\Error\Warning $e) { + self::fail('Should not throw an exception'); + } + + self::assertEmpty($sut->getDiscoveredNamespaces()); + } /** *