Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent cfca38f commit eb006e5
Show file tree
Hide file tree
Showing 109 changed files with 823 additions and 1,131 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ext-json": "*",
"php": ">=8.1",
"myclabs/deep-copy": "^1.9",
"spiral/core": "^3.14.10"
"spiral/core": "^3.15"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
9 changes: 4 additions & 5 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ final class Builder
public function __construct(
private readonly LoaderInterface $loader,
private readonly Parser $parser = new Parser(),
private readonly Compiler $compiler = new Compiler()
) {
}
private readonly Compiler $compiler = new Compiler(),
) {}

public function getLoader(): LoaderInterface
{
Expand Down Expand Up @@ -104,7 +103,7 @@ public function load(string $path): Template
$tpl = $this->parser->withPath($path)->parse($stream);
$tpl->setContext(new Context(
new Token(Token::TYPE_RAW, 0, ''),
$path
$path,
));
} catch (ParserException $e) {
throw $this->mapException($e);
Expand Down Expand Up @@ -163,7 +162,7 @@ private function mapException(ContextExceptionInterface $e): ContextExceptionInt

$e->setLocation(
$source->getFilename(),
Source::resolveLine($source->getContent(), $e->getContext()->getToken()->offset)
Source::resolveLine($source->getContent(), $e->getContext()->getToken()->offset),
);

return $e;
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function compile(array|NodeInterface $node, ?Result $result = null): Resu

throw new CompilerException(
\sprintf('Unable to compile %s, no renderer found', $node::class),
$node->getContext()
$node->getContext(),
);
}
}
7 changes: 3 additions & 4 deletions src/Compiler/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ public function __construct(
public string $path,
public int $offset,
public ?string $grammar = null,
public ?Location $parent = null
) {
}
public ?Location $parent = null,
) {}

public static function fromContext(Context $context, ?Location $parent = null): Location
{
return new self(
$context->getPath(),
$context->getToken()->offset,
$context->getToken()->grammar,
$parent
$parent,
);
}
}
6 changes: 3 additions & 3 deletions src/Compiler/Renderer/CoreRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class CoreRenderer implements RendererInterface
public function render(
Compiler $compiler,
Compiler\Result $result,
NodeInterface $node
NodeInterface $node,
): bool {
switch (true) {
case $node instanceof Hidden:
Expand All @@ -32,7 +32,7 @@ function (Compiler\Result $source) use ($node, $compiler): void {
foreach ($node->nodes as $child) {
$compiler->compile($child, $source);
}
}
},
);

return true;
Expand All @@ -49,7 +49,7 @@ function (Compiler\Result $source) use ($node, $compiler): void {

$compiler->compile($child, $source);
}
}
},
);

return true;
Expand Down
9 changes: 4 additions & 5 deletions src/Compiler/Renderer/DynamicRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ final class DynamicRenderer implements Compiler\RendererInterface

public function __construct(
private readonly ?DirectiveRendererInterface $directiveRenderer = null,
private readonly string $defaultFilter = self::DEFAULT_FILTER
) {
}
private readonly string $defaultFilter = self::DEFAULT_FILTER,
) {}

public function render(Compiler $compiler, Compiler\Result $result, NodeInterface $node): bool
{
Expand Down Expand Up @@ -51,7 +50,7 @@ private function directive(Compiler\Result $source, Directive $directive): void

throw new DirectiveException(
\sprintf('Undefined directive `%s`', $directive->name),
$directive->getContext()
$directive->getContext(),
);
}

Expand All @@ -66,7 +65,7 @@ private function output(Compiler\Result $source, Output $output): void

$source->push(
\sprintf(\sprintf('<?php echo %s; ?>', $filter), \trim((string) $output->body)),
$output->getContext()
$output->getContext(),
);
}
}
45 changes: 23 additions & 22 deletions src/Compiler/SourceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ final class SourceMap
{
/** @var Source[]|null */
private ?array $sourceCache = null;

private array $paths = [];
private array $lines = [];

public function __serialize(): array
public static function calculate(string $content, array $locations, LoaderInterface $loader): SourceMap
{
return [
'paths' => $this->paths,
'lines' => $this->lines,
];
}
$map = new self();

public function __unserialize(array $data): void
{
$this->paths = $data['paths'];
$this->lines = $data['lines'];
foreach ($locations as $offset => $location) {
$line = Source::resolveLine($content, $offset);
if (!isset($map->lines[$line])) {
$map->lines[$line] = $map->calculateLine($location, $loader);
}
}

$map->sourceCache = null;

return $map;
}

/**
Expand Down Expand Up @@ -83,20 +86,18 @@ public function unserialize(string $serialized): void
$this->__unserialize(\json_decode($serialized, true));
}

public static function calculate(string $content, array $locations, LoaderInterface $loader): SourceMap
public function __serialize(): array
{
$map = new self();

foreach ($locations as $offset => $location) {
$line = Source::resolveLine($content, $offset);
if (!isset($map->lines[$line])) {
$map->lines[$line] = $map->calculateLine($location, $loader);
}
}

$map->sourceCache = null;
return [
'paths' => $this->paths,
'lines' => $this->lines,
];
}

return $map;
public function __unserialize(array $data): void
{
$this->paths = $data['paths'];
$this->lines = $data['lines'];
}

private function unpack(array &$result, array $line): void
Expand Down
5 changes: 2 additions & 3 deletions src/Directive/DirectiveGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ final class DirectiveGroup implements DirectiveRendererInterface
* @param \Spiral\Stempler\Directive\DirectiveRendererInterface[] $directives
*/
public function __construct(
private array $directives = []
) {
}
private array $directives = [],
) {}

/**
* Add new directive(s) compiler.
Expand Down
2 changes: 1 addition & 1 deletion src/Directive/JsonDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function renderJson(Directive $directive): string
'<?php echo json_encode(%s, %s, %s) ?>',
$directive->values[0] ?? $directive->body,
$directive->values[1] ?? self::DEFAULT_OPTIONS,
$directive->values[2] ?? 512
$directive->values[2] ?? 512,
);
}
}
4 changes: 1 addition & 3 deletions src/Exception/DirectiveException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Stempler\Exception;

class DirectiveException extends CompilerException
{
}
class DirectiveException extends CompilerException {}
4 changes: 1 addition & 3 deletions src/Exception/LoaderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Stempler\Exception;

class LoaderException extends \RuntimeException
{
}
class LoaderException extends \RuntimeException {}
4 changes: 1 addition & 3 deletions src/Exception/ScannerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Stempler\Exception;

class ScannerException extends \RuntimeException
{
}
class ScannerException extends \RuntimeException {}
2 changes: 1 addition & 1 deletion src/Exception/SyntaxException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SyntaxException extends \RuntimeException
{
public function __construct(
string $message,
private readonly Token $token
private readonly Token $token,
) {
$message = \sprintf('%s at offset %s', $message, $token->offset);
parent::__construct($message, 0, null);
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/Traits/ContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait ContextTrait
public function __construct(
string $message,
private Context $context,
?\Throwable $previous = null
?\Throwable $previous = null,
) {
parent::__construct($message, 0, $previous);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Lexer/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function __construct(
/** @internal */
private readonly \Generator $generator,
private int $offset = 0,
) {
}
) {}

/**
* Delegate generation to the nested generator and collect
Expand Down
5 changes: 2 additions & 3 deletions src/Lexer/Byte.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class Byte
{
public function __construct(
public int $offset,
public string $char
) {
}
public string $char,
) {}
}
6 changes: 3 additions & 3 deletions src/Lexer/Grammar/Dynamic/BracesGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
string $startSequence,
string $endSequence,
private readonly int $startToken,
private readonly int $endToken
private readonly int $endToken,
) {
$this->setStartSequence($startSequence);
$this->setEndSequence($endSequence);
Expand Down Expand Up @@ -92,7 +92,7 @@ public function parse(Buffer $src, Byte $n): ?array
new Token(
$this->startToken,
$n->offset,
$n->char . $this->nextBytes($src, \strlen((string) $this->startSequence) - 1)
$n->char . $this->nextBytes($src, \strlen((string) $this->startSequence) - 1),
),
];

Expand Down Expand Up @@ -125,7 +125,7 @@ public function parse(Buffer $src, Byte $n): ?array
$this->tokens[] = new Token(
$this->endToken,
$n->offset,
$n->char . $this->nextBytes($src, \strlen((string) $this->endSequence) - 1)
$n->char . $this->nextBytes($src, \strlen((string) $this->endSequence) - 1),
);

break 2;
Expand Down
22 changes: 11 additions & 11 deletions src/Lexer/Grammar/Dynamic/DeclareGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ final class DeclareGrammar implements GrammarInterface

private array $keyword = [];

public static function tokenName(int $token): string
{
return match ($token) {
self::TYPE_KEYWORD => 'DECLARE:KEYWORD',
self::TYPE_EQUAL => 'DECLARE:EQUAL',
self::TYPE_COMMA => 'DECLARE:COMMA',
self::TYPE_QUOTED => 'DECLARE:QUOTED',
default => 'DECLARE:UNDEFINED',
};
}

/**
* TODO issue #767
* @link https://github.com/spiral/framework/issues/767
Expand Down Expand Up @@ -90,15 +101,4 @@ public function parse(Buffer $src): \Generator
yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
}
}

public static function tokenName(int $token): string
{
return match ($token) {
self::TYPE_KEYWORD => 'DECLARE:KEYWORD',
self::TYPE_EQUAL => 'DECLARE:EQUAL',
self::TYPE_COMMA => 'DECLARE:COMMA',
self::TYPE_QUOTED => 'DECLARE:QUOTED',
default => 'DECLARE:UNDEFINED',
};
}
}
Loading

0 comments on commit eb006e5

Please sign in to comment.