Skip to content
Permalink
Browse files

Remove ability to return false instead of throwing exception from ->g…

…et(), add types
  • Loading branch information
samdark committed Mar 12, 2020
1 parent aa65903 commit 763f4bd98c858d78c3b92f44f25e2c3908a21a24
Showing with 10 additions and 19 deletions.
  1. +7 −13 src/Aliases.php
  2. +3 −6 tests/AliasesTest.php
@@ -4,7 +4,7 @@

final class Aliases
{
private $aliases = [];
private array $aliases = [];

/**
* @param array $config
@@ -24,7 +24,7 @@ public function __construct(array $config = [])
* @param string $name
* @param string $value
*/
public function __set($name, $value)
public function __set(string $name, string $value): void
{
$this->set($name, $value);
}
@@ -122,13 +122,11 @@ public function set(string $alias, ?string $path): void
* See the [guide article on aliases](guide:concept-aliases) for more information.
*
* @param string $alias the alias to be translated.
* @param bool $throwException whether to throw an exception if the given alias is invalid.
* If this is false and an invalid alias is given, false will be returned by this method.
* @return string|bool the path corresponding to the alias, false if the root alias is not previously registered.
* @throws \InvalidArgumentException if the alias is invalid while $throwException is true.
* @return string the path corresponding to the alias.
* @throws \InvalidArgumentException if the root alias is not previously registered.
* @see setAlias()
*/
public function get($alias, $throwException = true)
public function get(string $alias): string
{
if (!$this->isAlias($alias)) {
return $alias;
@@ -140,11 +138,7 @@ public function get($alias, $throwException = true)
return $result['path'];
}

if ($throwException) {
throw new \InvalidArgumentException("Invalid path alias: $alias");
}

return false;
throw new \InvalidArgumentException("Invalid path alias: $alias");
}

/**
@@ -154,7 +148,7 @@ public function get($alias, $throwException = true)
* @param string $alias the alias
* @return string the root alias, or null if no root alias is found
*/
public function getRoot($alias): ?string
public function getRoot(string $alias): ?string
{
$result = $this->findAlias($alias);
if (\is_array($result)) {
@@ -5,14 +5,12 @@
use PHPUnit\Framework\TestCase;
use Yiisoft\Aliases\Aliases;

class AliasesTest extends TestCase
final class AliasesTest extends TestCase
{
public function testGet(): void
{
$aliases = new Aliases();

$this->assertFalse($aliases->get('@nonexisting', false));

$aliasNotBeginsWithAt = 'alias not begins with @';
$this->assertEquals($aliasNotBeginsWithAt, $aliases->get($aliasNotBeginsWithAt));

@@ -29,7 +27,6 @@ public function testGet(): void
$this->assertEquals('/yii/framework/test', $aliases->get('@tii'));

$aliases->set('@yii', null);
$this->assertFalse($aliases->get('@yii', false));
$this->assertEquals('/yii/gii/file', $aliases->get('@yii/gii/file'));

$aliases->set('@some/alias', '/www');
@@ -38,7 +35,7 @@ public function testGet(): void
$erroneousAlias = '@alias_not_exists';
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Invalid path alias: %s', $erroneousAlias));
$aliases->get($erroneousAlias, true);
$aliases->get($erroneousAlias);
}

public function testGetRoot(): void
@@ -79,6 +76,6 @@ public function testConstructConfig(): void
$erroneousAlias = '@alias_not_exists';
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Invalid path alias: %s', $erroneousAlias));
$aliases->get($erroneousAlias, true);
$aliases->get($erroneousAlias);
}
}

0 comments on commit 763f4bd

Please sign in to comment.
You can’t perform that action at this time.