Skip to content

Commit

Permalink
misc: remove unneeded internal checks and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Dec 22, 2022
1 parent 210e63f commit 1577233
Show file tree
Hide file tree
Showing 29 changed files with 11 additions and 522 deletions.
22 changes: 0 additions & 22 deletions src/Definition/Exception/InvalidParameterIndex.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Definition/Exception/MethodNotFound.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Definition/Exception/ParameterNotFound.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Definition/Exception/PropertyNotFound.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Definition/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace CuyZ\Valinor\Definition;

use Countable;
use CuyZ\Valinor\Definition\Exception\MethodNotFound;
use IteratorAggregate;
use Traversable;

Expand Down Expand Up @@ -33,10 +32,6 @@ public function has(string $name): bool

public function get(string $name): MethodDefinition
{
if (! $this->has($name)) {
throw new MethodNotFound($name);
}

return $this->methods[$name];
}

Expand Down
10 changes: 0 additions & 10 deletions src/Definition/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace CuyZ\Valinor\Definition;

use Countable;
use CuyZ\Valinor\Definition\Exception\InvalidParameterIndex;
use CuyZ\Valinor\Definition\Exception\ParameterNotFound;
use IteratorAggregate;
use Traversable;

Expand Down Expand Up @@ -36,10 +34,6 @@ public function has(string $name): bool

public function get(string $name): ParameterDefinition
{
if (! $this->has($name)) {
throw new ParameterNotFound($name);
}

return $this->parameters[$name];
}

Expand All @@ -48,10 +42,6 @@ public function get(string $name): ParameterDefinition
*/
public function at(int $index): ParameterDefinition
{
if ($index >= $this->count()) {
throw new InvalidParameterIndex($index, $this);
}

return array_values($this->parameters)[$index];
}

Expand Down
5 changes: 0 additions & 5 deletions src/Definition/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace CuyZ\Valinor\Definition;

use Countable;
use CuyZ\Valinor\Definition\Exception\PropertyNotFound;
use IteratorAggregate;
use Traversable;

Expand Down Expand Up @@ -33,10 +32,6 @@ public function has(string $name): bool

public function get(string $name): PropertyDefinition
{
if (! $this->has($name)) {
throw new PropertyNotFound($name);
}

return $this->properties[$name];
}

Expand Down
20 changes: 0 additions & 20 deletions src/Mapper/Object/Exception/MissingMethodArgument.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Mapper/Object/Exception/MissingPropertyArgument.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/Mapper/Object/MethodArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace CuyZ\Valinor\Mapper\Object;

use CuyZ\Valinor\Definition\Parameters;
use CuyZ\Valinor\Mapper\Object\Exception\MissingMethodArgument;
use IteratorAggregate;
use Traversable;

Expand All @@ -29,10 +28,6 @@ public function __construct(Parameters $parameters, array $arguments)
foreach ($parameters as $parameter) {
$name = $parameter->name();

if (! array_key_exists($parameter->name(), $arguments) && ! $parameter->isOptional()) {
throw new MissingMethodArgument($parameter);
}

if ($parameter->isVariadic()) {
$this->arguments = [...$this->arguments, ...array_values($arguments[$name])]; // @phpstan-ignore-line we know that the argument is iterable
} else {
Expand Down
8 changes: 0 additions & 8 deletions src/Mapper/Object/ReflectionObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace CuyZ\Valinor\Mapper\Object;

use CuyZ\Valinor\Definition\ClassDefinition;
use CuyZ\Valinor\Mapper\Object\Exception\MissingPropertyArgument;

use function array_key_exists;
use function count;

/** @internal */
Expand All @@ -26,12 +24,6 @@ public function describeArguments(): Arguments

public function build(array $arguments): object
{
foreach ($this->class->properties() as $property) {
if (! array_key_exists($property->name(), $arguments) && ! $property->hasDefaultValue()) {
throw new MissingPropertyArgument($property);
}
}

$object = new ($this->class->name())();

if (count($arguments) > 0) {
Expand Down
19 changes: 0 additions & 19 deletions src/Mapper/Tree/Exception/CannotGetParentOfRootShell.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Mapper/Tree/Exception/ShellHasNoValue.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/Mapper/Tree/Exception/UnresolvableShellType.php

This file was deleted.

23 changes: 4 additions & 19 deletions src/Mapper/Tree/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

use CuyZ\Valinor\Definition\Attributes;
use CuyZ\Valinor\Definition\AttributesContainer;
use CuyZ\Valinor\Mapper\Tree\Exception\CannotGetParentOfRootShell;
use CuyZ\Valinor\Mapper\Tree\Exception\NewShellTypeDoesNotMatch;
use CuyZ\Valinor\Mapper\Tree\Exception\ShellHasNoValue;
use CuyZ\Valinor\Mapper\Tree\Exception\UnresolvableShellType;
use CuyZ\Valinor\Type\Type;
use CuyZ\Valinor\Type\Types\UnresolvableType;

use function array_unshift;
use function assert;
use function implode;

/** @internal */
Expand All @@ -31,9 +29,7 @@ final class Shell

private function __construct(private Type $type)
{
if ($type instanceof UnresolvableType) {
throw new UnresolvableShellType($type);
}
assert(! $type instanceof UnresolvableType);

This comment has been minimized.

Copy link
@danog

danog Dec 22, 2022

Contributor

Note that the assert function may not always work, depending on how it is configured in php.ini.
In fact, the default configuration specifies to only emit a warning, not throw an exception.

This comment has been minimized.

Copy link
@romm

romm Dec 22, 2022

Author Member

This is intended; in production this check should be totally ignored. during the development of the library, this can help identify issues. 🙂

This comment has been minimized.

Copy link
@romm

romm Dec 22, 2022

Author Member

In this particular case, the condition should NEVER be true; if that is, the assertion should fail and warn me that something is wrong in the library (not its usage).

}

public static function root(Type $type, mixed $value): self
Expand Down Expand Up @@ -64,22 +60,13 @@ public function isRoot(): bool
return ! isset($this->parent);
}

public function parent(): self
{
if (! isset($this->parent)) {
throw new CannotGetParentOfRootShell();
}

return $this->parent;
}

public function withType(Type $newType): self
{
$clone = clone $this;
$clone->type = $newType;

if (! $newType->matches($this->type)) {
throw new NewShellTypeDoesNotMatch($this, $newType);
throw new NewShellTypeDoesNotMatch($this, $newType); // @todo remove?
}

return $clone;
Expand All @@ -106,9 +93,7 @@ public function hasValue(): bool

public function value(): mixed
{
if (! $this->hasValue) {
throw new ShellHasNoValue();
}
assert($this->hasValue);

return $this->value;
}
Expand Down
19 changes: 0 additions & 19 deletions src/Type/Parser/Exception/Stream/TryingToAccessOutboundToken.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Type/Parser/Exception/Stream/TryingToReadFinishedStream.php

This file was deleted.

Loading

0 comments on commit 1577233

Please sign in to comment.