Skip to content

Commit

Permalink
Up phpstan baseline
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Sep 11, 2024
1 parent 6ae9989 commit 1261f65
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 188 deletions.
3 changes: 0 additions & 3 deletions src/Compiler/Markers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class Markers extends PCRECompiler
* @param array<array-key, string> $chunks
*
* @return non-empty-string
*
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
protected function buildTokens(array $chunks): string
{
Expand Down
5 changes: 2 additions & 3 deletions src/Compiler/PCRECompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ abstract class PCRECompiler implements CompilerInterface
*/
public function __construct(?array $flags = null, ?bool $debug = null)
{
/** @psalm-suppress PropertyTypeCoercion */
$this->flags = $flags ?? self::DEFAULT_FLAGS;

if ($debug === null) {
Expand Down Expand Up @@ -269,7 +268,7 @@ protected function test(string $pattern, ?string $original = null): void

@\preg_match_all($this->wrap($pattern), '', $matches, $flags);

if ($error = \error_get_last()) {
if (($error = \error_get_last()) !== null) {
throw new CompilationException($this->formatException($error['message'], $original));
}
}
Expand All @@ -293,6 +292,6 @@ protected function formatException(string $message, ?string $token = null): stri
$message = \preg_replace('/([\w_]+\(\):\h+)/', '', $message);
$message = \preg_replace('/\h*at\h+offset\h+\d+/', '', $message);

return \ucfirst($message) . (\is_string($token) ? $suffix : '');
return \ucfirst((string) $message) . (\is_string($token) ? $suffix : '');
}
}
14 changes: 3 additions & 11 deletions src/Config/PassthroughWhenTokenHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@
*/
final class PassthroughWhenTokenHandler implements HandlerInterface
{
/**
* @var non-empty-string
*
* @readonly
*/
private string $name;

/**
* @param non-empty-string $name
*/
public function __construct(string $name)
{
$this->name = $name;
}
public function __construct(
private readonly string $name,
) {}

public function handle(ReadableInterface $source, TokenInterface $token): ?TokenInterface
{
Expand Down
9 changes: 3 additions & 6 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ abstract class Driver implements DriverInterface
*/
private ?string $pattern = null;

private CompilerInterface $compiler;

public function __construct(CompilerInterface $compiler)
{
$this->compiler = $compiler;
}
public function __construct(
private readonly CompilerInterface $compiler,
) {}

public function reset(): void
{
Expand Down
12 changes: 1 addition & 11 deletions src/Driver/Markers.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,10 @@ class Markers extends Driver
*/
private const UNKNOWN_PATTERN = '.+?';

/**
* @var non-empty-string
*
* @readonly
*/
private string $unknown;

public function __construct(
?MarkersCompiler $compiler = null,
string $unknown = self::UNKNOWN_TOKEN_NAME
private readonly string $unknown = self::UNKNOWN_TOKEN_NAME,
) {
$this->unknown = $unknown;

parent::__construct($compiler ?? new MarkersCompiler());
}

Expand All @@ -59,7 +50,6 @@ public function run(array $tokens, ReadableInterface $source, int $offset = 0):
/** @var non-empty-string $name */
$name = \array_pop($payload);

/** @psalm-suppress InvalidArgument */
yield $this->make($name, $payload);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Exception/EndlessRecursionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

class EndlessRecursionException extends UnexpectedStateException
{
public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
{
public static function fromState(
mixed $state,
ReadableInterface $src,
?TokenInterface $tok,
?\Throwable $e = null,
): self {
$message = \vsprintf('An unsolvable infinite lexer state transitions was found at %s', [
$state,
]);
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/UnexpectedStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public static function fromEmptyStates(ReadableInterface $src, ?\Throwable $e =
}

/**
* @param string|int $state
* @param array-key $state
*/
public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
public static function fromState(string|int $state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
{
$message = \sprintf('Unrecognized token state #%s', $state);

Expand Down
113 changes: 47 additions & 66 deletions src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,15 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface
* Default token name for unidentified tokens.
*
* @var non-empty-string
*
* @final
*/
public const DEFAULT_UNKNOWN_TOKEN_NAME = UnknownToken::DEFAULT_TOKEN_NAME;
final public const DEFAULT_UNKNOWN_TOKEN_NAME = UnknownToken::DEFAULT_TOKEN_NAME;

/**
* Default token name for end of input.
*
* @var non-empty-string
*
* @final
*/
public const DEFAULT_EOI_TOKEN_NAME = EndOfInput::DEFAULT_TOKEN_NAME;

/**
* @var array<array-key, non-empty-string>
*/
protected array $tokens = [];

/**
* @var list<non-empty-string>
*/
protected array $skip = [];
final public const DEFAULT_EOI_TOKEN_NAME = EndOfInput::DEFAULT_TOKEN_NAME;

private DriverInterface $driver;

Expand All @@ -63,29 +49,9 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface

private HandlerInterface $onEndOfInput;

/**
* @var non-empty-string
*
* @readonly
*/
private string $unknown;
private readonly SourceFactoryInterface $sources;

/**
* @var non-empty-string
*
* @readonly
*/
private string $eoi;

/**
* @readonly
*/
private SourceFactoryInterface $sources;

/**
* @param array<array-key, non-empty-string> $tokens list of
* token names/identifiers and its patterns
* @param list<array-key> $skip list of hidden token names/identifiers
* @param HandlerInterface $onUnknownToken This setting is responsible for
* the behavior of the lexer in case of detection of unrecognized
* tokens.
Expand All @@ -109,36 +75,43 @@ class Lexer implements PositionalLexerInterface, MutableLexerInterface
*
* Note that you can also define your own {@see HandlerInterface} to
* override behavior.
* @param non-empty-string $unknown The identifier that marks each unknown
* token inside the executor (internal runtime). This parameter only
* needs to be changed if the name is already in use in the user's
* token set (in the {@see $tokens} parameter), otherwise it makes
* no sense.
* @param non-empty-string $eoi
*/
public function __construct(
array $tokens = [],
array $skip = [],
/**
* List of token names/identifiers and its patterns.
*
* @var array<array-key, non-empty-string>
*/
protected array $tokens = [],
/**
* List of hidden token names/identifiers.
*
* @var list<array-key>
*/
protected array $skip = [],
?DriverInterface $driver = null,
?HandlerInterface $onHiddenToken = null,
?HandlerInterface $onUnknownToken = null,
?HandlerInterface $onEndOfInput = null,
string $unknown = Lexer::DEFAULT_UNKNOWN_TOKEN_NAME,
string $eoi = Lexer::DEFAULT_EOI_TOKEN_NAME,
?SourceFactoryInterface $sources = null
/**
* The identifier that marks each unknown token inside the executor
* (internal runtime). This parameter only needs to be changed if the
* name is already in use in the user's token set (in the {@see $tokens}
* parameter), otherwise it makes no sense.
*
* @var non-empty-string
*/
private readonly string $unknown = Lexer::DEFAULT_UNKNOWN_TOKEN_NAME,
/**
* @var non-empty-string
*/
private readonly string $eoi = Lexer::DEFAULT_EOI_TOKEN_NAME,
?SourceFactoryInterface $sources = null,
) {
$this->tokens = $tokens;
$this->skip = $skip;

$this->driver = $driver ?? new Markers(new MarkersCompiler(), $unknown);

$this->eoi = $eoi;
$this->unknown = $unknown;

$this->onHiddenToken = $onHiddenToken ?? new NullHandler();
$this->onUnknownToken = $onUnknownToken ?? new ThrowErrorHandler();
$this->onEndOfInput = $onEndOfInput ?? new PassthroughHandler();

$this->sources = $sources ?? new SourceFactory();
}

Expand Down Expand Up @@ -319,7 +292,7 @@ public function remove(string ...$tokens): self
* starting the lexical analysis and indicates problems in the
* analyzed source
*/
public function lex($source, int $offset = 0): iterable
public function lex(mixed $source, int $offset = 0): iterable
{
try {
$source = $this->sources->create($source);
Expand All @@ -346,9 +319,13 @@ public function lex($source, int $offset = 0): iterable
continue;
}

if ($unknown !== [] && ($result = $this->handleUnknownToken($source, $unknown))) {
yield $result;
$unknown = [];
if ($unknown !== []) {
$result = $this->handleUnknownToken($source, $unknown);

if ($result !== null) {
yield $result;
$unknown = [];
}
}

yield $token;
Expand All @@ -357,11 +334,17 @@ public function lex($source, int $offset = 0): iterable
throw LexerException::fromInternalError($e);
}

if ($unknown !== [] && $result = $this->handleUnknownToken($source, $unknown)) {
yield $token = $result;
if ($unknown !== []) {
$result = $this->handleUnknownToken($source, $unknown);

if ($result !== null) {
yield $token = $result;
}
}

if (($eoi = $this->handleEoiToken($source, $token ?? null)) !== null) {
$eoi = $this->handleEoiToken($source, $token ?? null);

if ($eoi !== null) {
yield $eoi;
}
}
Expand All @@ -386,9 +369,7 @@ private function handleEoiToken(ReadableInterface $source, ?TokenInterface $last
*/
private function reduceUnknownToken(array $tokens): TokenInterface
{
$concat = static function (string $data, TokenInterface $token): string {
return $data . $token->getValue();
};
$concat = static fn(string $data, TokenInterface $token): string => $data . $token->getValue();

$value = \array_reduce($tokens, $concat, '');

Expand Down
Loading

0 comments on commit 1261f65

Please sign in to comment.