Skip to content

Commit

Permalink
Add default (impossible) branch
Browse files Browse the repository at this point in the history
  • Loading branch information
olsavmic committed Sep 6, 2024
1 parent 4c8b4b4 commit 1a7b801
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Compiler/Mapper/Object/MapDiscriminatedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShipMonk\InputMapper\Compiler\Mapper\Object;

use Attribute;
use LogicException;
use Nette\Utils\Arrays;
use PhpParser\Node\Expr;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand Down Expand Up @@ -109,6 +110,16 @@ public function compile(Expr $value, Expr $path, PhpCodeBuilder $builder): Compi
);
}

$subtypeMatchArms[] = $builder->matchArm(
null,
$builder->throwExpr(
$builder->new(
$builder->importClass(LogicException::class),
['Impossible case detected. Please report this as a bug.'],
),
),
);

$matchedSubtype = $builder->match($discriminatorMapperCall, $subtypeMatchArms);

return new CompiledExpr(
Expand Down
6 changes: 6 additions & 0 deletions src/Compiler/Php/PhpCodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Throw_ as ThrowExpr_;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -278,6 +279,11 @@ public function throw(Expr $expr): Throw_
return new Throw_($expr);
}

public function throwExpr(Expr $expr): ThrowExpr_
{
return new ThrowExpr_($expr);
}

public function assign(Expr $var, Expr $expr): Expression
{
return new Expression(new Assign($var, $expr));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data;

use LogicException;
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapDiscriminatedObject;
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
use ShipMonk\InputMapper\Runtime\Mapper;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function map(mixed $data, array $path = []): HierarchicalParentInput
return match ($this->mapType($data['type'], [...$path, 'type'])) {
'childOne' => $this->provider->get(HierarchicalChildOneInput::class)->map($data, $path),
'childTwo' => $this->provider->get(HierarchicalChildTwoInput::class)->map($data, $path),
default => throw new LogicException('Impossible case detected. Please report this as a bug.'),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data;

use LogicException;
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapDiscriminatedObject;
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
use ShipMonk\InputMapper\Runtime\Mapper;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function map(mixed $data, array $path = []): HierarchicalWithEnumParentIn

return match ($this->mapType($data['type'], [...$path, 'type'])) {
'childOne' => $this->provider->get(HierarchicalWithEnumChildInput::class)->map($data, $path),
default => throw new LogicException('Impossible case detected. Please report this as a bug.'),
};
}

Expand Down

0 comments on commit 1a7b801

Please sign in to comment.