Skip to content

Commit

Permalink
Merge pull request #22 from intaro/phpstan-level-8
Browse files Browse the repository at this point in the history
phpstan up to level 8
  • Loading branch information
muxx authored Sep 15, 2024
2 parents 001ab69 + c1b13b2 commit 209d359
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Annotation/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class Sandbox
*
* List of allowed types is defined in `intaro.twig_sandbox.sandbox_annotation.value_types` parameter
*/
public $type = 'string';
public string $type = 'string';
}
28 changes: 22 additions & 6 deletions Builder/EnvironmentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
class EnvironmentBuilder implements WarmableInterface
{
private LoaderInterface $loader;
private $options;
/** @var array<string, mixed> */
private array $options;
private ?SecurityPolicy $policy;
private $rules;
private ?SecurityPolicyRules $rules = null;
/** @var AbstractExtension[] */
private array $extensions = [];
private DumperInterface $dumper;

/**
* @param array<string, mixed> $options
*/
public function __construct(LoaderInterface $loader, DumperInterface $dumper, SecurityPolicy $policy = null, array $options = [])
{
$this->loader = $loader;
Expand All @@ -44,7 +49,7 @@ public function addExtension(AbstractExtension $extension): void
/**
* @param AbstractExtension[]|null $extensions
*/
public function addExtensions(array $extensions = null): void
public function addExtensions(?array $extensions = null): void
{
if (!is_array($extensions)) {
return;
Expand All @@ -57,8 +62,10 @@ public function addExtensions(array $extensions = null): void

/**
* Формирует окружение для Twig Sandbox
*
* @param array<string, mixed> $params
*/
public function getSandboxEnvironment($params = [], SecurityPolicy $securityPolicy = null): TwigAdapter
public function getSandboxEnvironment(array $params = [], SecurityPolicy $securityPolicy = null): TwigAdapter
{
$loader = new ArrayLoader();
$twig = new Environment($loader, $params);
Expand All @@ -78,6 +85,9 @@ public function getSandboxEnvironment($params = [], SecurityPolicy $securityPoli
return new TwigAdapter($twig);
}

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): void
{
$this->options = [
Expand All @@ -102,6 +112,9 @@ public function setOptions(array $options): void
}
}

/**
* @phpstan-assert SecurityPolicy $this->policy
*/
private function initSecurityPolicy(): void
{
$rules = $this->getPolicyRules();
Expand All @@ -114,7 +127,7 @@ private function initSecurityPolicy(): void
$this->policy->setAllowedMethods($rules->getMethods());
}

public function getPolicyRules()
public function getPolicyRules(): SecurityPolicyRules
{
if ($this->rules) {
return $this->rules;
Expand All @@ -135,7 +148,7 @@ public function getPolicyRules()
foreach ($this->options['bundles'] as $bundle) {
$refl = new \ReflectionClass($bundle);

$dir = dirname($refl->getFileName()) . '/Entity';
$dir = dirname((string) $refl->getFileName()) . '/Entity';
if (file_exists($dir) && is_dir($dir)) {
$rules->merge($this->loader->load($dir));
}
Expand All @@ -149,6 +162,9 @@ public function getPolicyRules()
return $this->rules;
}

/**
* @param string $cacheDir
*/
public function warmUp(/*string */ $cacheDir/*, ?string $buildDir = null*/)/*: array*/
{
$currentDir = $this->options['cache_dir'];
Expand Down
24 changes: 18 additions & 6 deletions Builder/TwigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Twig\Environment;

/**
* @mixin Environment
*/
class TwigAdapter
{
private Environment $twigEnvironment;
Expand All @@ -13,20 +16,29 @@ public function __construct(Environment $twigEnvironment)
$this->twigEnvironment = $twigEnvironment;
}

/**
* @param mixed[] $args
*
* @return mixed
*/
public function __call(string $method, array $args)
{
if (method_exists($this, $method)) {
return $this->{$method(...$args)};
return $this->$method(...$args);
}

return $this->twigEnvironment->{$method}(...$args);
return $this->twigEnvironment->$method(...$args);
}

public function render($template, array $context = []): string
/**
* @param array<string, mixed> $context
*/
public function render(string $template, array $context = []): string
{
$template = $this->twigEnvironment->createTemplate($template);

return $template->render($context);
return $this->twigEnvironment
->createTemplate($template)
->render($context)
;
}

public function getTwig(): Environment
Expand Down
12 changes: 5 additions & 7 deletions CacheWarmer/TwigSandboxCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@

use Intaro\TwigSandboxBundle\Builder\EnvironmentBuilder;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;

class TwigSandboxCacheWarmer implements CacheWarmerInterface
{
protected $environmentBuilder;
protected EnvironmentBuilder $environmentBuilder;

public function __construct(EnvironmentBuilder $builder)
{
$this->environmentBuilder = $builder;
}

/**
* @param string $cacheDir
*/
public function warmUp(/*string */ $cacheDir/*, ?string $buildDir = null*/)/*: array*/
{
if ($this->environmentBuilder instanceof WarmableInterface) {
return $this->environmentBuilder->warmUp($cacheDir);
}

return [];
return $this->environmentBuilder->warmUp($cacheDir);
}

public function isOptional(): bool
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/IntaroTwigSandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
class IntaroTwigSandboxExtension extends Extension
{
/**
* {@inheritDoc}
* @param array<mixed> $configs
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
Expand Down
2 changes: 1 addition & 1 deletion Dumper/DumperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface DumperInterface
{
public function dump(SecurityPolicyRules $rules);
public function dump(SecurityPolicyRules $rules): string;
}
2 changes: 1 addition & 1 deletion Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class PhpDumper implements DumperInterface
{
public function dump(SecurityPolicyRules $rules)
public function dump(SecurityPolicyRules $rules): string
{
$result = "<?php\n";
$result .= "return new Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules(\n";
Expand Down
12 changes: 6 additions & 6 deletions Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

class AnnotationClassLoader implements LoaderInterface
{
protected $reader;
protected $annotationClass = 'Intaro\\TwigSandboxBundle\\Annotation\\Sandbox';
protected Reader $reader;
protected string $annotationClass = 'Intaro\\TwigSandboxBundle\\Annotation\\Sandbox';

public function __construct(Reader $reader)
{
Expand All @@ -23,7 +23,7 @@ public function __construct(Reader $reader)
*
* @param string $class A fully-qualified class name
*/
public function setAnnotationClass($class): void
public function setAnnotationClass(string $class): void
{
$this->annotationClass = $class;
}
Expand All @@ -38,7 +38,7 @@ public function setAnnotationClass($class): void
*
* @throws \InvalidArgumentException When annotations can't be parsed
*/
public function load($class, $type = null)
public function load($class, $type = null): SecurityPolicyRules
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
Expand All @@ -50,7 +50,7 @@ public function load($class, $type = null)
}

$rules = new SecurityPolicyRules();
$rules->addResource(new FileResource($class->getFileName()));
$rules->addResource(new FileResource((string) $class->getFileName()));

foreach ($class->getMethods() as $method) {
foreach ($this->reader->getMethodAnnotations($method) as $annot) {
Expand All @@ -73,7 +73,7 @@ public function load($class, $type = null)
}

/**
* {@inheritdoc}
* @param ?string $type
*/
public function supports($resource, $type = null): bool
{
Expand Down
8 changes: 4 additions & 4 deletions Loader/AnnotationDirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class AnnotationDirectoryLoader extends AnnotationFileLoader
/**
* Loads from annotations from a directory.
*
* @param string $path A directory path
* @param string $type The resource type
* @param string $path A directory path
* @param ?string $type The resource type
*
* @return SecurityPolicyRules A Rules instance
*
* @throws \InvalidArgumentException When annotations can't be parsed
*/
public function load($path, $type = null)
public function load($path, $type = null): SecurityPolicyRules
{
$dir = $this->locator->locate($path);

Expand Down Expand Up @@ -47,7 +47,7 @@ public function load($path, $type = null)
}

/**
* {@inheritdoc}
* @param ?string $type
*/
public function supports($resource, $type = null): bool
{
Expand Down
19 changes: 11 additions & 8 deletions Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class AnnotationFileLoader extends FileLoader
{
protected $loader;
protected AnnotationClassLoader $loader;

/**
* Constructor.
Expand All @@ -31,14 +31,14 @@ public function __construct(FileLocator $locator, AnnotationClassLoader $loader,
/**
* Loads from annotations from a file.
*
* @param string $file A PHP file path
* @param string $type The resource type
* @param string $file A PHP file path
* @param ?string $type The resource type
*
* @return SecurityPolicyRules A Rules instance
*
* @throws \InvalidArgumentException When annotations can't be parsed
*/
public function load($file, $type = null)
public function load($file, $type = null): SecurityPolicyRules
{
$path = $this->locator->locate($file);

Expand All @@ -52,7 +52,7 @@ public function load($file, $type = null)
}

/**
* {@inheritdoc}
* @param ?string $type
*/
public function supports($resource, $type = null): bool
{
Expand All @@ -64,13 +64,13 @@ public function supports($resource, $type = null): bool
*
* @param string $file A PHP file path
*
* @return string|false Full class name if found, false otherwise
* @return class-string|false Full class name if found, false otherwise
*/
protected function findClass(string $file)
{
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
$tokens = token_get_all((string) file_get_contents($file));

if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
Expand All @@ -89,7 +89,10 @@ protected function findClass(string $file)
}

if (true === $class && \T_STRING === $token[0]) {
return $namespace . '\\' . $token[1];
/** @var class-string $r */
$r = $namespace . '\\' . $token[1];

return $r;
}

if (true === $namespace && isset($nsTokens[$token[0]])) {
Expand Down
Loading

0 comments on commit 209d359

Please sign in to comment.