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 d2124f5 commit e126e5d
Show file tree
Hide file tree
Showing 30 changed files with 151 additions and 182 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
],
"require": {
"php": ">=8.1",
"spiral/core": "^3.14.10",
"spiral/files": "^3.14.10",
"spiral/core": "^3.15",
"spiral/files": "^3.15",
"psr/event-dispatcher": "^1.0"
},
"require-dev": {
"spiral/boot": "^3.14.10",
"spiral/boot": "^3.15",
"phpunit/phpunit": "^10.1",
"mockery/mockery": "^1.5",
"vimeo/psalm": "^5.9"
Expand Down
17 changes: 8 additions & 9 deletions src/Bootloader/ViewsBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ final class ViewsBootloader extends Bootloader
];

public function __construct(
private readonly ConfiguratorInterface $config
) {
}
private readonly ConfiguratorInterface $config,
) {}

public function init(EnvironmentInterface $env, DirectoriesInterface $dirs, DebugMode $debugMode): void
{
Expand All @@ -57,7 +56,7 @@ public function init(EnvironmentInterface $env, DirectoriesInterface $dirs, Debu
],
'dependencies' => [],
'engines' => [NativeEngine::class],
]
],
);
}

Expand All @@ -69,37 +68,37 @@ public function addDirectory(string $namespace, string $directory): void

$this->config->modify(
ViewsConfig::CONFIG,
new Append('namespaces.' . $namespace, null, $directory)
new Append('namespaces.' . $namespace, null, $directory),
);
}

public function addEngine(string|EngineInterface $engine): void
{
$this->config->modify(
ViewsConfig::CONFIG,
new Append('engines', null, $engine)
new Append('engines', null, $engine),
);
}

public function addCacheDependency(string|DependencyInterface $dependency): void
{
$this->config->modify(
ViewsConfig::CONFIG,
new Append('dependencies', null, $dependency)
new Append('dependencies', null, $dependency),
);
}

protected function initGlobalVariables(ViewsConfig $config): GlobalVariablesInterface
{
return new GlobalVariables(
$config->getGlobalVariables()
$config->getGlobalVariables(),
);
}

protected function initLoader(ViewsConfig $config): LoaderInterface
{
return new ViewLoader(
$config->getNamespaces()
$config->getNamespaces(),
);
}
}
8 changes: 4 additions & 4 deletions src/Config/ViewsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function getNamespaces(): array
public function getDependencies(): array
{
return \array_map(
fn (mixed $dependency): Autowire => $this->wire($dependency),
(array) ($this->config['dependencies'] ?? [])
fn(mixed $dependency): Autowire => $this->wire($dependency),
(array) ($this->config['dependencies'] ?? []),
);
}

Expand All @@ -69,8 +69,8 @@ public function getDependencies(): array
public function getEngines(): array
{
return \array_map(
fn (mixed $engine): Autowire => $this->wire($engine),
(array) ($this->config['engines'] ?? [])
fn(mixed $engine): Autowire => $this->wire($engine),
(array) ($this->config['engines'] ?? []),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Context/ValueDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ValueDependency implements DependencyInterface
public function __construct(
private readonly string $name,
private readonly mixed $value,
?array $variants = null
?array $variants = null,
) {
$this->variants = $variants ?? [$value];
}
Expand Down
5 changes: 2 additions & 3 deletions src/ContextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
final class ContextGenerator
{
public function __construct(
private readonly ContextInterface $context
) {
}
private readonly ContextInterface $context,
) {}

/**
* Generate all possible context variations.
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Native/NativeEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class NativeEngine extends AbstractEngine

public function __construct(
private readonly ContainerInterface $container,
string $extension = self::EXTENSION
string $extension = self::EXTENSION,
) {
$this->extension = $extension;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Engine/Native/NativeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ final class NativeView implements ViewInterface
{
public function __construct(
private readonly ViewSource $view,
private readonly ContainerInterface $container
) {
}
private readonly ContainerInterface $container,
) {}

public function render(array $data = []): string
{
Expand Down
3 changes: 1 addition & 2 deletions src/Event/ViewNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ final class ViewNotFound
{
public function __construct(
public readonly string $path,
) {
}
) {}
}
4 changes: 1 addition & 3 deletions src/Exception/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Views\Exception;

class CacheException extends ViewException
{
}
class CacheException extends ViewException {}
4 changes: 1 addition & 3 deletions src/Exception/ContextException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Context specific exceptions.
*/
class ContextException extends ViewException
{
}
class ContextException extends ViewException {}
4 changes: 1 addition & 3 deletions src/Exception/EngineException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Views\Exception;

class EngineException extends ViewException
{
}
class EngineException extends ViewException {}
4 changes: 1 addition & 3 deletions src/Exception/LoaderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Errors while view loading.
*/
class LoaderException extends ViewException
{
}
class LoaderException extends ViewException {}
4 changes: 1 addition & 3 deletions src/Exception/PathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Views\Exception;

class PathException extends LoaderException
{
}
class PathException extends LoaderException {}
4 changes: 1 addition & 3 deletions src/Exception/ViewException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Generic view component exception (view file not found and etc).
*/
class ViewException extends \RuntimeException
{
}
class ViewException extends \RuntimeException {}
5 changes: 2 additions & 3 deletions src/GlobalVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
final class GlobalVariables implements GlobalVariablesInterface
{
public function __construct(
private array $variables = []
) {
}
private array $variables = [],
) {}

public function set(string $name, mixed $data): void
{
Expand Down
15 changes: 7 additions & 8 deletions src/Loader/PathParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ final class PathParser
{
public function __construct(
private readonly string $defaultNamespace,
private readonly string $extension
) {
}
private readonly string $extension,
) {}

public function getExtension(): string
{
Expand Down Expand Up @@ -46,7 +45,7 @@ public function parse(string $path): ?ViewPath
$filename = \preg_replace(
'#/{2,}#',
'/',
\str_replace('\\', '/', $path)
\str_replace('\\', '/', $path),
);

$namespace = $this->defaultNamespace;
Expand Down Expand Up @@ -75,7 +74,7 @@ public function parse(string $path): ?ViewPath
return new ViewPath(
$namespace,
$this->fetchName($filename),
$filename
$filename,
);
}

Expand Down Expand Up @@ -107,16 +106,16 @@ private function validatePath(string $path): void
$level = 0;

foreach ($parts as $part) {
if ('..' === $part) {
if ($part === '..') {
--$level;
} elseif ('.' !== $part) {
} elseif ($part !== '.') {
++$level;
}

if ($level < 0) {
throw new PathException(\sprintf(
'Looks like you try to load a view outside configured directories (%s)',
$path
$path,
));
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Loader/ViewPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ final class ViewPath
public function __construct(
private readonly string $namespace,
private readonly string $name,
private readonly string $basename
) {
}
private readonly string $basename,
) {}

public function getNamespace(): string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/ContextProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static function (array $matches) use ($context) {
return $matches['default'] ?? throw $e;
}
},
$source->getCode()
$source->getCode(),
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ViewLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function exists(string $path, ?string &$filename = null, ?ViewPath &$pars
return false;
}

foreach ((array)$this->namespaces[$parsed->getNamespace()] as $directory) {
foreach ((array) $this->namespaces[$parsed->getNamespace()] as $directory) {
$directory = $this->files->normalizePath($directory, true);
if ($this->files->exists(\sprintf('%s%s', $directory, $parsed->getBasename()))) {
$filename = \sprintf('%s%s', $directory, $parsed->getBasename());
Expand All @@ -88,7 +88,7 @@ public function load(string $path): ViewSource
return new ViewSource(
$filename,
$parsed->getNamespace(),
$parsed->getName()
$parsed->getName(),
);
}

Expand All @@ -104,7 +104,7 @@ public function list(?string $namespace = null): array
continue;
}

foreach ((array)$directories as $directory) {
foreach ((array) $directories as $directory) {
$files = $this->files->getFiles($directory);

foreach ($files as $filename) {
Expand Down
8 changes: 3 additions & 5 deletions src/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private readonly ViewsConfig $config,
private readonly GlobalVariablesInterface $globalVariables,
FactoryInterface $factory,
?ContextInterface $context = null
?ContextInterface $context = null,
) {
$this->context = $context ?? new ViewContext();
$this->loader = $factory->make(LoaderInterface::class, [
Expand Down Expand Up @@ -61,9 +61,9 @@ public function addEngine(EngineInterface $engine): void
{
$this->engines[] = $engine->withLoader($this->loader);

\uasort($this->engines, static fn (EngineInterface $a, EngineInterface $b) => \strcmp(
\uasort($this->engines, static fn(EngineInterface $a, EngineInterface $b): int => \strcmp(
$a->getLoader()->getExtension() ?? '',
$b->getLoader()->getExtension() ?? ''
$b->getLoader()->getExtension() ?? '',
));

$this->engines = \array_values($this->engines);
Expand All @@ -82,7 +82,6 @@ public function getEngines(): array
/**
* Compile one of multiple cache versions for a given view path.
*
*
* @throws ViewException
*/
public function compile(string $path): void
Expand Down Expand Up @@ -145,7 +144,6 @@ public function render(string $path, array $data = []): string
}

/**
*
* @throws ViewException
*/
private function findEngine(string $path): EngineInterface
Expand Down
5 changes: 2 additions & 3 deletions src/ViewSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ final class ViewSource
public function __construct(
private readonly string $filename,
private readonly string $namespace,
private readonly string $name
) {
}
private readonly string $name,
) {}

/**
* Template namespace.
Expand Down
Loading

0 comments on commit e126e5d

Please sign in to comment.