diff --git a/CHANGELOG.md b/CHANGELOG.md index 62a50621be..53a3b3fe75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). [#1986](https://github.com/phalcon/zephir/issues/1986) - Fixed incorrect namespace on type hinted return when generating API docs [#1229](https://github.com/phalcon/zephir/issues/1229) +- Fixed incorrect stubs generation for classes in the same namespace + [#2016](https://github.com/phalcon/zephir/issues/2016) ## [0.12.10] - 2019-10-19 ### Fixed diff --git a/Library/AliasManager.php b/Library/AliasManager.php index bef31e3aa8..03b37926f5 100644 --- a/Library/AliasManager.php +++ b/Library/AliasManager.php @@ -113,7 +113,10 @@ public function isAliasPresentFor(string $className): bool { $extractAlias = $this->implicitAlias($className); - return !isset($this->aliases[$extractAlias]); + $isClassDeclarated = \in_array($className, $this->aliases); + $classAlias = array_flip($this->aliases)[$className] ?? null; + + return $isClassDeclarated && $classAlias !== $extractAlias; } /** diff --git a/unit-tests/Zephir/Test/AliasManagerTest.php b/unit-tests/Zephir/Test/AliasManagerTest.php index c72b0aa7be..1bb35f025a 100644 --- a/unit-tests/Zephir/Test/AliasManagerTest.php +++ b/unit-tests/Zephir/Test/AliasManagerTest.php @@ -54,16 +54,21 @@ public function baseTestSuiteProvider(): array ], 'without explicit alias' => [ [ - 'name' => 'Throwable', - 'alias' => 'Throwable', + 'name' => '\\Throwable', + ], + ], + 'without explicit alias and FQN' => [ + [ + 'name' => 'Zephir\\Compiler\\CompilerInterface', ], ], ]; } - public function aliasDataProvider(): array + public function aliasProvider(): array { $expected = [ + // [ alias => name ] [ 'EventsManagerInterface' => 'Bug\\Events\\ManagerInterface', ], @@ -71,7 +76,10 @@ public function aliasDataProvider(): array 'EventsManagerInterface' => '\\Bug\\Events\\ManagerInterface', ], [ - 'Throwable' => 'Throwable', + 'Throwable' => '\Throwable', + ], + [ + 'CompilerInterface' => 'Zephir\\Compiler\\CompilerInterface', ], ]; @@ -80,7 +88,7 @@ public function aliasDataProvider(): array /** * @test - * @dataProvider aliasDataProvider + * @dataProvider aliasProvider */ public function shouldProperAddStatements(array $useStatements, array $expected) { @@ -88,18 +96,20 @@ public function shouldProperAddStatements(array $useStatements, array $expected) 'aliases' => [$useStatements], ]); - $alias = $useStatements['alias']; $className = $useStatements['name']; + $parts = explode('\\', $className); + $alias = $useStatements['alias'] ?? $parts[\count($parts) - 1]; + $this->assertTrue($this->testAliasMgr->isAlias($alias)); - $this->assertSame($this->testAliasMgr->getAliases(), $expected); - $this->assertSame($this->testAliasMgr->getAlias($alias), $className); + $this->assertSame($expected, $this->testAliasMgr->getAliases()); + $this->assertSame($className, $this->testAliasMgr->getAlias($alias)); } - public function statementDataProvider(): array + public function statementProvider(): array { $expected = [ - true, true, false, + true, true, false, false, ]; return $this->injectExpectedResult($expected); @@ -107,7 +117,7 @@ public function statementDataProvider(): array /** * @test - * @dataProvider statementDataProvider + * @dataProvider statementProvider */ public function shouldCheckAliasedStatement(array $useStatements, bool $expected) { @@ -115,11 +125,13 @@ public function shouldCheckAliasedStatement(array $useStatements, bool $expected 'aliases' => [$useStatements], ]); - $alias = $useStatements['alias']; $className = $useStatements['name']; - $this->assertSame($this->testAliasMgr->isUseStatementAliased($alias), $expected); - $this->assertSame($this->testAliasMgr->isAliasPresentFor($className), $expected); + $parts = explode('\\', $className); + $alias = $useStatements['alias'] ?? $parts[\count($parts) - 1]; + + $this->assertSame($expected, $this->testAliasMgr->isUseStatementAliased($alias)); + $this->assertSame($expected, $this->testAliasMgr->isAliasPresentFor($className)); } public function classNameDataProvider(): array @@ -127,7 +139,8 @@ public function classNameDataProvider(): array $expected = [ 'EventsManagerInterface', '\Bug\Events\ManagerInterface', - 'Throwable', + '\Throwable', + 'CompilerInterface', ]; return $this->injectExpectedResult($expected); @@ -145,6 +158,40 @@ public function shouldGetAliasForClassName(array $useStatements, string $expecte $className = $useStatements['name']; - $this->assertSame($this->testAliasMgr->getAliasForClassName($className), $expected); + $this->assertSame($expected, $this->testAliasMgr->getAliasForClassName($className)); + } + + /** @test */ + public function shouldCheckIfAliasPresentForClass() + { + $this->testAliasMgr->add([ + 'aliases' => [ + [ + 'name' => 'One', + 'alias' => 'One', + ], + [ + 'name' => 'Bug\\Events\\ManagerInterface', + 'alias' => 'EventsManagerInterface', + ], + [ + 'name' => '\\Root\SomeNamespace\\SomeClassName', + 'alias' => 'SomeClassName', + ], + [ + 'name' => 'AnotherClass', + 'alias' => 'AnotherClass', + ], + [ + 'name' => 'Bug\\Storage\\FileSystem', + ], + ], + ]); + + $this->assertTrue($this->testAliasMgr->isAliasPresentFor('Bug\\Events\\ManagerInterface')); + $this->assertFalse($this->testAliasMgr->isAliasPresentFor('\\Root\SomeNamespace\\SomeClassName')); + $this->assertFalse($this->testAliasMgr->isAliasPresentFor('AnotherClass')); + $this->assertFalse($this->testAliasMgr->isAliasPresentFor('NonExistingClass')); + $this->assertFalse($this->testAliasMgr->isAliasPresentFor('Bug\\Storage\\FileSystem')); } }