Skip to content

Commit

Permalink
Fix #35: Don't throw TypeError from method getAll(), when alias is …
Browse files Browse the repository at this point in the history
…nested
  • Loading branch information
Fantom409 committed Nov 28, 2020
1 parent a47611d commit d62f473
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -3,13 +3,13 @@

## 1.1.2 under development

- no changes in this release.
- Bug #35: Don't throw TypeError from method `getAll()`, when alias is nested (Fantom409)


## 1.1.1 November 06, 2020


- Fix #28: Add `params` to `config-plugin` section in `composer.json` (vjik)
- Bug #28: Add `params` to `config-plugin` section in `composer.json` (vjik)



Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="6"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
15 changes: 14 additions & 1 deletion src/Aliases.php
Expand Up @@ -6,10 +6,15 @@

final class Aliases
{
/**
* @var array
* @psalm-var array<string, string|array<string, string>>
*/
private array $aliases = [];

/**
* @param array $config
* @psalm-param array<string, string> $config
*
* @throws \InvalidArgumentException if $path is an invalid alias.
*
Expand Down Expand Up @@ -67,6 +72,7 @@ public function set(string $alias, string $path): void
$alias = '@' . $alias;
}
$pos = strpos($alias, '/');
/** @psalm-var string $root */
$root = $pos === false ? $alias : substr($alias, 0, $pos);

$path = rtrim($path, '\\/');
Expand Down Expand Up @@ -193,8 +199,15 @@ public function getAll(): array
{
$result = [];
foreach ($this->aliases as $name => $path) {
$result[$name] = $this->get($path);
if (is_array($path)) {
foreach ($path as $innerName => $innerPath) {
$result[$innerName] = $innerPath;
}
} else {
$result[$name] = $this->get($path);
}
}

return $result;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/AliasesTest.php
Expand Up @@ -156,5 +156,15 @@ public function testGetAll(): void
'@yii' => '/yii',
];
$this->assertEquals($expected, $aliases->getAll());

$expected = [
'@yii' => '/yii/framework',
'@yii/gii' => '/yii/gii',
'@yii/gii/assets' => '/yii/gii_assets',
];

$aliases = new Aliases($expected);

$this->assertEquals($expected, $aliases->getAll());
}
}

0 comments on commit d62f473

Please sign in to comment.