Skip to content

Commit 6c9dfdc

Browse files
authored
Merge pull request #265 from php-java/add-operand-validation
Add operand validation
2 parents f2485ee + a6b5327 commit 6c9dfdc

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Compiler/Builder/Generator/Operation/Operand.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,45 @@
22
declare(strict_types=1);
33
namespace PHPJava\Compiler\Builder\Generator\Operation;
44

5+
use PHPJava\Compiler\Builder\Finder\Result\ConstantPoolFinderResult;
6+
use PHPJava\Compiler\Builder\Types\Int16;
7+
use PHPJava\Compiler\Builder\Types\Int32;
8+
use PHPJava\Compiler\Builder\Types\Int8;
9+
use PHPJava\Compiler\Builder\Types\Uint16;
10+
use PHPJava\Compiler\Builder\Types\Uint32;
11+
use PHPJava\Compiler\Builder\Types\Uint8;
12+
use PHPJava\Exceptions\AssembleStructureException;
13+
514
class Operand
615
{
716
protected $type;
817
protected $value;
918

1019
public static function factory(string $type, $value): self
1120
{
21+
if (!in_array(
22+
$type,
23+
[
24+
Uint8::class,
25+
Uint16::class,
26+
Uint32::class,
27+
Int8::class,
28+
Int16::class,
29+
Int32::class,
30+
],
31+
true
32+
)) {
33+
throw new AssembleStructureException(
34+
'Invalid class type: ' . $type
35+
);
36+
}
37+
if (!($value instanceof ConstantPoolFinderResult)
38+
&& !is_int($value)
39+
) {
40+
throw new AssembleStructureException(
41+
'Passed parameter is not initiated a ConstantPoolFinderResult'
42+
);
43+
}
1244
$scopedValue = $value;
1345
return new static($type, $scopedValue);
1446
}

0 commit comments

Comments
 (0)