Skip to content

Commit

Permalink
Ensure all throwables are documented and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed May 31, 2021
1 parent 8ac5da7 commit 460d156
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"nette/schema": "^1.1"
},
"require-dev": {
"phpstan/phpstan": "^0.12.70",
"phpstan/phpstan": "^0.12.88",
"phpunit/phpunit": "^8.5 || ^9.2",
"scrutinizer/ocular": "^1.5",
"unleashedtech/php-coding-standard": "^3.0",
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ parameters:
level: max
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true
implicitThrows: false
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
15 changes: 13 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace League\Config;

use Dflydev\DotAccessData\Data;
use Dflydev\DotAccessData\Exception\DataException;
use Dflydev\DotAccessData\Exception\InvalidPathException;
use Dflydev\DotAccessData\Exception\MissingPathException;
use League\Config\Exception\UnknownOptionException;
Expand Down Expand Up @@ -104,7 +105,11 @@ public function set(string $key, $value): void
{
$this->invalidate();

$this->userConfig->set($key, $value);
try {
$this->userConfig->set($key, $value);
} catch (DataException $ex) {
throw new UnknownOptionException($ex->getMessage(), $key, (int) $ex->getCode(), $ex);
}
}

/**
Expand Down Expand Up @@ -140,7 +145,11 @@ public function exists(string $key): bool
return true;
}

return $this->finalConfig->has($key);
try {
return $this->finalConfig->has($key);
} catch (InvalidPathException $ex) {
return false;
}
}

/**
Expand All @@ -163,6 +172,8 @@ private function invalidate(): void
/**
* Applies the schema against the configuration to return the final configuration
*
* @throws ValidationException
*
* @psalm-allow-private-mutation
*/
private function build(): Data
Expand Down
6 changes: 5 additions & 1 deletion src/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface ConfigurationInterface
/**
* @param string $key Configuration option path/key
*
* @psalm-param non-empty-string $key
*
* @return mixed
*
* @throws ValidationException if the schema failed to validate the given input
Expand All @@ -34,9 +36,11 @@ public function get(string $key);
/**
* @param string $key Configuration option path/key
*
* @psalm-param non-empty-string $key
*
* @return bool Whether the given option exists
*
* @throw ValidationException if the schema failed to validate the given input
* @throws ValidationException if the schema failed to validate the given input
*/
public function exists(string $key): bool;
}
4 changes: 4 additions & 0 deletions src/MutableConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace League\Config;

use League\Config\Exception\UnknownOptionException;

/**
* Interface for setting/merging user-defined configuration values into the configuration object
*/
interface MutableConfigurationInterface
{
/**
* @param mixed $value
*
* @throws UnknownOptionException if $key contains a nested path which doesn't point to an array value
*/
public function set(string $key, $value): void;

Expand Down
14 changes: 14 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ public function testExists(): void

// Re-test 'str' to check the cache
$this->assertTrue($config->exists('str'));

// Test with an empty string
$this->assertFalse($config->exists(''));
}

public function testSet(): void
Expand Down Expand Up @@ -215,6 +218,17 @@ public function testSetUndefinedKey(): void
$config->get('foo');
}

public function testSetNestedWhenOptionNotNested(): void
{
$this->expectException(UnknownOptionException::class);
$this->expectExceptionMessageMatches('/cannot be indexed into/');

$config = new Configuration(['foo' => Expect::int(42)]);

$config->set('foo', 3);
$config->set('foo.bar', 42);
}

public function testMerge(): void
{
$config = new Configuration([
Expand Down

0 comments on commit 460d156

Please sign in to comment.