Skip to content

Commit 2cf976e

Browse files
committed
Fix some psalm errors
1 parent 603a4a6 commit 2cf976e

File tree

7 files changed

+92
-71
lines changed

7 files changed

+92
-71
lines changed

src/Directive/Directive.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ abstract class Directive implements FunctionLikeDirectiveInterface
1818
/**
1919
* @var string
2020
*/
21-
protected const ERROR_TOO_MANY_ARGUMENTS = 'Too many arguments when macro directive is called, %s required';
21+
private const ERROR_TOO_MANY_ARGUMENTS = 'Too many arguments when macro directive is called, %s required';
2222

2323
/**
2424
* @var string
2525
*/
26-
protected const ERROR_TOO_FEW_ARGUMENTS = 'Too few arguments when macro directive is called, %s required';
26+
private const ERROR_TOO_FEW_ARGUMENTS = 'Too few arguments when macro directive is called, %s required';
2727

2828
/**
29-
* @var int
29+
* @var positive-int|0
3030
*/
3131
protected int $minArgumentsCount = 0;
3232

3333
/**
34-
* @var int
34+
* @var positive-int|0
3535
*/
3636
protected int $maxArgumentsCount = 0;
3737

@@ -53,11 +53,11 @@ protected function assertArgumentsCount(array $arguments): void
5353
$haystack = \count($arguments);
5454

5555
if ($haystack > $this->getMaxArgumentsCount()) {
56-
throw new \ArgumentCountError(\sprintf(static::ERROR_TOO_MANY_ARGUMENTS, $this->getMaxArgumentsCount()));
56+
throw new \ArgumentCountError(\sprintf(self::ERROR_TOO_MANY_ARGUMENTS, $this->getMaxArgumentsCount()));
5757
}
5858

5959
if ($haystack < $this->getMinArgumentsCount()) {
60-
throw new \ArgumentCountError(\sprintf(static::ERROR_TOO_FEW_ARGUMENTS, $this->getMinArgumentsCount()));
60+
throw new \ArgumentCountError(\sprintf(self::ERROR_TOO_FEW_ARGUMENTS, $this->getMinArgumentsCount()));
6161
}
6262
}
6363

src/Internal/Expression/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static function fromFile(string $pathname): self
7979
* @return ExpressionInterface
8080
* @throws \Throwable
8181
*/
82-
public function parse($source, array $options = []): iterable
82+
public function parse($source, array $options = []): ExpressionInterface
8383
{
8484
return $this->runtime->parse($source, $options);
8585
}

src/Internal/Runtime/DirectiveExecutor.php

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

1414
use FFI\Contracts\Preprocessor\Directive\DirectiveInterface;
15+
use FFI\Contracts\Preprocessor\Directive\FunctionLikeDirectiveInterface;
1516
use FFI\Contracts\Preprocessor\Directive\RepositoryInterface;
1617
use FFI\Preprocessor\Exception\DirectiveEvaluationException;
1718
use FFI\Preprocessor\Exception\PreprocessException;
@@ -84,9 +85,7 @@ public function replace(string $body, int $ctx = self::CTX_SOURCE): string
8485
$body = $this->replaceDefinedExpression($body);
8586
}
8687

87-
$body = $this->replaceDefinedDirectives($body);
88-
89-
return $body;
88+
return $this->replaceDefinedDirectives($body);
9089
}
9190

9291
/**
@@ -110,7 +109,10 @@ public function replace(string $body, int $ctx = self::CTX_SOURCE): string
110109
*/
111110
private function replaceDefinedExpression(string $body): string
112111
{
113-
$lookup = fn(array $matches) => $this->directives->defined($matches[1]) ? 'true' : 'false';
112+
$lookup = fn (array $matches): string =>
113+
/** @psalm-suppress MixedArgument */
114+
$this->directives->defined($matches[1]) ? 'true' : 'false'
115+
;
114116

115117
return \preg_replace_callback(self::PCRE_DEFINED, $lookup, $body);
116118
}
@@ -120,21 +122,22 @@ private function replaceDefinedExpression(string $body): string
120122
*
121123
* @param string $body
122124
* @return string
125+
* @psalm-suppress MixedInferredReturnType
123126
*/
124127
private function replaceDefinedDirectives(string $body): string
125128
{
126129
$stream = $this->findAndReplace($body);
127130

128131
while ($stream->valid()) {
129-
[$name, $arguments] = [$stream->key(), $stream->current()];
130-
131132
try {
132-
$stream->send($this->execute($name, $arguments));
133+
/** @psalm-suppress MixedArgument */
134+
$stream->send($this->execute($stream->key(), $stream->current()));
133135
} catch (\Throwable $e) {
134136
$stream->throw($e);
135137
}
136138
}
137139

140+
/** @psalm-suppress MixedReturnStatement */
138141
return $stream->getReturn();
139142
}
140143

@@ -201,7 +204,8 @@ private function findDirectiveAndReplace(string $name, DirectiveInterface $direc
201204
// and means that do not need to do a lookahead to read
202205
// additional directive arguments.
203206
//
204-
$isSimpleDirective = $directive->getMaxArgumentsCount() === 0;
207+
$isSimpleDirective = !$directive instanceof FunctionLikeDirectiveInterface
208+
|| $directive->getMaxArgumentsCount() === 0;
205209

206210
$coroutine = $this->findDirectiveAndUpdateBody($name, $body);
207211

@@ -263,11 +267,11 @@ private function findDirectiveAndReplace(string $name, DirectiveInterface $direc
263267
* }
264268
* </code>
265269
*
266-
* @psalm-return \Generator<int, int, string, string>
267-
*
268270
* @param string $name
269271
* @param string $body
270272
* @return \Generator
273+
* @psalm-return \Generator<int, int, string, string>
274+
* @psalm-suppress MixedReturnTypeCoercion
271275
*/
272276
private function findDirectiveAndUpdateBody(string $name, string $body): \Generator
273277
{
@@ -285,6 +289,7 @@ private function findDirectiveAndUpdateBody(string $name, string $body): \Genera
285289
continue;
286290
}
287291

292+
/** @psalm-suppress RedundantConditionGivenDocblockType */
288293
if (\is_string($replacement = yield $offset => $offset + $length)) {
289294
$body = $replacement;
290295
}
@@ -444,7 +449,7 @@ private function createTokenForSource(string $name, string $body, int $from, int
444449
* Accepts the name of a directive and its arguments, and returns the
445450
* result of executing that directive.
446451
*
447-
* @param string $name
452+
* @param non-empty-string $name
448453
* @param array $arguments
449454
* @return string
450455
* @throws DirectiveEvaluationException

0 commit comments

Comments
 (0)