Skip to content

Commit 4f24f45

Browse files
committed
Added PHP 7.4 support
1 parent 306eff9 commit 4f24f45

15 files changed

+138
-68
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"license": "MIT",
55
"keywords": ["ffi", "parser", "compiler", "c", "headers", "preprocessor"],
66
"require": {
7-
"php": ">=8.0",
7+
"php": ">=7.4",
88
"psr/log": "^1.0",
99
"phplrt/runtime": "^3.1",
10+
"symfony/polyfill-php80": "^1.23",
1011
"symfony/polyfill-ctype": "^1.23"
1112
},
1213
"autoload": {
@@ -15,6 +16,7 @@
1516
}
1617
},
1718
"require-dev": {
19+
"jetbrains/phpstorm-attributes": "^1.0",
1820
"phpunit/phpunit": "^9.0",
1921
"monolog/monolog": "^2.1",
2022
"phplrt/phplrt": "^3.1"

src/Directive/Directive.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ public function getMinArgumentsCount(): int
7979
* @param mixed $result
8080
* @return string
8181
*/
82-
public static function render(mixed $result): string
82+
public static function render($result): string
8383
{
84-
return match(true) {
85-
$result === true => '1',
86-
\is_null($result), $result === false => '0',
87-
default => (string)$result,
88-
};
84+
switch (true) {
85+
case $result === true:
86+
return '1';
87+
case $result === null:
88+
case $result === false:
89+
return '0';
90+
default:
91+
return (string)$result;
92+
}
8993
}
9094
}

src/Directive/RegistrarInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface RegistrarInterface
2020
* @param mixed $value
2121
* @throws DirectiveDefinitionException
2222
*/
23-
public function define(string $directive, mixed $value = DirectiveInterface::DEFAULT_VALUE): void;
23+
public function define(string $directive, $value = DirectiveInterface::DEFAULT_VALUE): void;
2424

2525
/**
2626
* @param string $directive

src/Directive/Repository.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ final class Repository implements RepositoryInterface, RegistrarInterface
2222

2323
/**
2424
* @param iterable<string, DirectiveInterface> $directives
25-
* @throws \ReflectionException
2625
*/
2726
public function __construct(iterable $directives = [])
2827
{
@@ -36,20 +35,26 @@ public function __construct(iterable $directives = [])
3635
* @return DirectiveInterface
3736
* @throws \ReflectionException
3837
*/
39-
private function cast(mixed $directive): DirectiveInterface
38+
private function cast($directive): DirectiveInterface
4039
{
41-
return match (true) {
42-
$directive instanceof DirectiveInterface => $directive,
43-
\is_callable($directive) => new FunctionDirective($directive),
44-
\is_scalar($directive), $directive instanceof \Stringable => new ObjectLikeDirective((string)$directive),
45-
default => $directive,
46-
};
40+
switch (true) {
41+
case $directive instanceof DirectiveInterface:
42+
return $directive;
43+
case \is_callable($directive):
44+
return new FunctionDirective($directive);
45+
case \is_scalar($directive):
46+
case $directive instanceof \Stringable:
47+
case \is_object($directive) && \method_exists($directive, '__toString'):
48+
return new ObjectLikeDirective((string)$directive);
49+
default:
50+
return $directive;
51+
}
4752
}
4853

4954
/**
5055
* {@inheritDoc}
5156
*/
52-
public function define(string $directive, mixed $value = DirectiveInterface::DEFAULT_VALUE): void
57+
public function define(string $directive, $value = DirectiveInterface::DEFAULT_VALUE): void
5358
{
5459
try {
5560
$expr = $this->cast($value);

src/Internal/Expression/Ast/Value/Value.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct($value)
3636
* @param string $value
3737
* @return string
3838
*/
39-
protected static function parse(string $value)
39+
protected static function parse(string $value): string
4040
{
4141
return $value;
4242
}

src/Internal/Lexer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use FFI\Preprocessor\Internal\Lexer\Simplifier;
1515
use Phplrt\Contracts\Lexer\LexerInterface;
1616
use Phplrt\Contracts\Lexer\TokenInterface;
17-
use FFI\Preprocessor\Internal\ExpressionToken;
1817
use Phplrt\Lexer\Lexer as Runtime;
1918
use Phplrt\Lexer\Token\Token;
2019
use Phplrt\Source\File;

src/Internal/Runtime/DirectiveExecutor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ final class DirectiveExecutor
5151
*/
5252
private const PCRE_DEFINED = '/\bdefined\h*(?:\(\h*(\w+)\h*\)|(\w+))/ium';
5353

54+
/**
55+
* @var RepositoryInterface
56+
*/
57+
private RepositoryInterface $directives;
58+
5459
/**
5560
* @param RepositoryInterface $directives
5661
*/
57-
public function __construct(private RepositoryInterface $directives)
62+
public function __construct(RepositoryInterface $directives)
5863
{
64+
$this->directives = $directives;
5965
}
6066

6167
/**

src/Internal/Runtime/SourceExecutor.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use FFI\Preprocessor\Io\Directory\Repository as DirectoriesRepository;
2424
use FFI\Preprocessor\Io\Source\Repository as SourcesRepository;
2525
use FFI\Preprocessor\Option;
26-
use FFI\Preprocessor\Preprocessor;
2726
use JetBrains\PhpStorm\ExpectedValues;
2827
use Phplrt\Contracts\Exception\RuntimeExceptionInterface;
2928
use Phplrt\Contracts\Lexer\TokenInterface;
@@ -68,6 +67,31 @@ final class SourceExecutor
6867
*/
6968
private Parser $expressions;
7069

70+
/**
71+
* @var DirectivesRepository
72+
*/
73+
private DirectivesRepository $directives;
74+
75+
/**
76+
* @var DirectoriesRepository
77+
*/
78+
private DirectoriesRepository $directories;
79+
80+
/**
81+
* @var SourcesRepository
82+
*/
83+
private SourcesRepository $sources;
84+
85+
/**
86+
* @var LoggerInterface
87+
*/
88+
private LoggerInterface $logger;
89+
90+
/**
91+
* @var int-mask-of<OptionEnum>
92+
*/
93+
private int $options;
94+
7195
/**
7296
* @param DirectivesRepository $directives
7397
* @param DirectoriesRepository $directories
@@ -76,13 +100,19 @@ final class SourceExecutor
76100
* @param int-mask-of<OptionEnum> $options
77101
*/
78102
public function __construct(
79-
private DirectivesRepository $directives,
80-
private DirectoriesRepository $directories,
81-
private SourcesRepository $sources,
82-
private LoggerInterface $logger,
103+
DirectivesRepository $directives,
104+
DirectoriesRepository $directories,
105+
SourcesRepository $sources,
106+
LoggerInterface $logger,
83107
#[ExpectedValues(flagsFromClass: Option::class)]
84-
private int $options,
108+
int $options
85109
) {
110+
$this->directives = $directives;
111+
$this->directories = $directories;
112+
$this->sources = $sources;
113+
$this->logger = $logger;
114+
$this->options = $options;
115+
86116
$this->lexer = new Lexer();
87117
$this->stack = new OutputStack();
88118
$this->executor = new DirectiveExecutor($this->directives);

src/Io/Source/RegistrarInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface RegistrarInterface
2525
* @param bool $overwrite
2626
* @return bool
2727
*/
28-
public function add(string $file, mixed $source, bool $overwrite = false): bool;
28+
public function add(string $file, $source, bool $overwrite = false): bool;
2929

3030
/**
3131
* @param string $file

src/Io/Source/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(iterable $files = [])
4040
/**
4141
* {@inheritDoc}
4242
*/
43-
public function add(string $file, mixed $source, bool $overwrite = false): bool
43+
public function add(string $file, $source, bool $overwrite = false): bool
4444
{
4545
$file = Normalizer::normalize($file);
4646

src/Preprocessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function load(EnvironmentInterface $env): void
8787
* {@inheritDoc}
8888
*/
8989
public function process(
90-
mixed $source,
90+
$source,
9191
#[ExpectedValues(flagsFromClass: Option::class)]
9292
int $options = Option::NOTHING
9393
): Result {
@@ -130,7 +130,7 @@ public function getDirectories(): DirectoriesRepositoryInterface
130130
/**
131131
* {@inheritDoc}
132132
*/
133-
public function define(string $directive, mixed $value = DirectiveInterface::DEFAULT_VALUE): void
133+
public function define(string $directive, $value = DirectiveInterface::DEFAULT_VALUE): void
134134
{
135135
$this->directives->define($directive, $value);
136136
}
@@ -146,7 +146,7 @@ public function undef(string $directive): bool
146146
/**
147147
* {@inheritDoc}
148148
*/
149-
public function add(string $file, mixed $source, bool $overwrite = false): bool
149+
public function add(string $file, $source, bool $overwrite = false): bool
150150
{
151151
return $this->sources->add($file, $source, $overwrite);
152152
}

src/PreprocessorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ interface PreprocessorInterface extends
4242
* @throws PreprocessorException
4343
*/
4444
public function process(
45-
mixed $source,
45+
$source,
4646
#[ExpectedValues(flagsFromClass: Option::class)]
4747
int $options = Option::NOTHING
4848
): ResultInterface;

src/Result.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace FFI\Preprocessor;
1313

1414
use FFI\Preprocessor\Directive\Directive;
15-
use FFI\Preprocessor\Directive\DirectiveInterface;
1615
use FFI\Preprocessor\Directive\RepositoryInterface as DirectivesRepositoryInterface;
1716
use FFI\Preprocessor\Io\Directory\RepositoryInterface as DirectoriesRepositoryInterface;
1817
use FFI\Preprocessor\Io\Source\RepositoryInterface as SourcesRepositoryInterface;
@@ -40,6 +39,31 @@ final class Result implements ResultInterface
4039
*/
4140
private ?string $result = null;
4241

42+
/**
43+
* @var iterable<string>
44+
*/
45+
private iterable $stream;
46+
47+
/**
48+
* @var DirectivesRepositoryInterface
49+
*/
50+
private DirectivesRepositoryInterface $directives;
51+
52+
/**
53+
* @var DirectoriesRepositoryInterface
54+
*/
55+
private DirectoriesRepositoryInterface $directories;
56+
57+
/**
58+
* @var SourcesRepositoryInterface
59+
*/
60+
private SourcesRepositoryInterface $sources;
61+
62+
/**
63+
* @var int-mask-of<OptionEnum>
64+
*/
65+
private int $options;
66+
4367
/**
4468
* @param iterable<string> $stream
4569
* @param DirectivesRepositoryInterface $directives
@@ -48,13 +72,18 @@ final class Result implements ResultInterface
4872
* @param int-mask-of<OptionEnum> $options
4973
*/
5074
public function __construct(
51-
private iterable $stream,
52-
private DirectivesRepositoryInterface $directives,
53-
private DirectoriesRepositoryInterface $directories,
54-
private SourcesRepositoryInterface $sources,
75+
iterable $stream,
76+
DirectivesRepositoryInterface $directives,
77+
DirectoriesRepositoryInterface $directories,
78+
SourcesRepositoryInterface $sources,
5579
#[ExpectedValues(flagsFromClass: Option::class)]
56-
private int $options = 0
80+
int $options = 0
5781
) {
82+
$this->stream = $stream;
83+
$this->directives = $directives;
84+
$this->directories = $directories;
85+
$this->sources = $sources;
86+
$this->options = $options;
5887
}
5988

6089
/**
@@ -153,13 +182,17 @@ private function injectBuiltinDirectives(string $result): string
153182
*/
154183
public function __get(string $property): \Traversable
155184
{
156-
return match ($property) {
157-
'sources' => $this->getSources(),
158-
'directives' => $this->getDirectives(),
159-
'directories' => $this->getDirectories(),
160-
default => throw new \LogicException(
161-
\sprintf('Undefined property: %s::$%s', self::class, $property)
162-
)
163-
};
185+
switch ($property) {
186+
case 'sources':
187+
return $this->getSources();
188+
case 'directives':
189+
return $this->getDirectives();
190+
case 'directories':
191+
return $this->getDirectories();
192+
default:
193+
throw new \LogicException(
194+
\sprintf('Undefined property: %s::$%s', self::class, $property)
195+
);
196+
}
164197
}
165198
}

0 commit comments

Comments
 (0)