Skip to content

Commit

Permalink
php cs (#15)
Browse files Browse the repository at this point in the history
* php cs
  • Loading branch information
addfs authored Jun 15, 2022
1 parent 6350b8b commit 3f5df8b
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ jobs:

- name: "Run static-analysis"
run: composer static-analysis

- name: "Run code-style check"
run: composer phpcs
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ vendor
/.idea
/.idea/*
.travis.yml

.php_cs.cache
.phpunit.result.cache
69 changes: 69 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/Annotation')
->in(__DIR__ . '/Builder')
->in(__DIR__ . '/CacheWarmer')
->in(__DIR__ . '/DependencyInjection')
->in(__DIR__ . '/Dumper')
->in(__DIR__ . '/Loader')
->in(__DIR__ . '/SecurityPolicy')
->in(__DIR__ . '/Validator')
->in(__DIR__ . '/tests');

$rules = [
'@Symfony' => true,
'@Symfony:risky' => true,

// exceptions
'single_line_throw' => false,

// php file
'concat_space' => ['spacing' => 'one'],

// namespace and imports
'global_namespace_import' => [
'import_classes' => false,
'import_constants' => false,
'import_functions' => false,
],

// standard functions and operators
'native_constant_invocation' => false,
'native_function_invocation' => false,

// functions, methods
'void_return' => true,

// operators
// multiline operators must always be at the beginning of the line
'operator_linebreak' => [
'only_booleans' => true,
'position' => 'beginning',
],
// Use null coalescing operator ?? where possible
'ternary_to_null_coalescing' => true,
'list_syntax' => ['syntax' => 'short'],

// phpdoc
'phpdoc_annotation_without_dot' => false,
'phpdoc_summary' => false,

// phpunit
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],

// semicolon
// move the semicolon to the new line for chained calls
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'],

'visibility_required' => ['elements' => ['property', 'method']],
];

return (new \PhpCsFixer\Config())
->setFinder($finder)
->setCacheFile(__DIR__ . '/.php_cs.cache')
->setRiskyAllowed(true)
->setRules($rules)
;
8 changes: 4 additions & 4 deletions Annotation/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Intaro\TwigSandboxBundle\Annotation;

/**
* @Annotation
* @Target({"METHOD", "PROPERTY"})
*/
* @Annotation
* @Target({"METHOD", "PROPERTY"})
*/
class Sandbox
{
/**
Expand All @@ -14,4 +14,4 @@ class Sandbox
* List of allowed types is defined in `intaro.twig_sandbox.sandbox_annotation.value_types` parameter
*/
public $type = 'string';
}
}
24 changes: 8 additions & 16 deletions Builder/EnvironmentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Intaro\TwigSandboxBundle\Builder;

use Intaro\TwigSandboxBundle\Dumper\DumperInterface;
use Intaro\TwigSandboxBundle\Dumper\PhpDumper;
use Symfony\Component\Config\Loader\LoaderInterface;
use Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Extension\SandboxExtension;
Expand Down Expand Up @@ -36,20 +35,14 @@ public function __construct(LoaderInterface $loader, DumperInterface $dumper, Se

/**
* Additional extension for sandbox environment
*
* @access public
* @param AbstractExtension $extension
* @return void
*/
public function addExtension(AbstractExtension $extension)
public function addExtension(AbstractExtension $extension): void
{
$this->extensions[] = $extension;
}

/**
* @param AbstractExtension[]|array|null $extensions
* @return void
*
*/
public function addExtensions(array $extensions = null): void
{
Expand Down Expand Up @@ -85,13 +78,13 @@ public function getSandboxEnvironment($params = [], SecurityPolicy $securityPoli
return new TwigAdapter($twig);
}

public function setOptions(array $options)
public function setOptions(array $options): void
{
$this->options = [
'cache_dir' => null,
'cache_dir' => null,
'cache_filename' => 'IntaroTwigSandboxPolicy',
'bundles' => [],
'debug' => false,
'bundles' => [],
'debug' => false,
];

// check option names and live merge, if errors are encountered Exception will be thrown
Expand All @@ -109,7 +102,7 @@ public function setOptions(array $options)
}
}

private function initSecurityPolicy()
private function initSecurityPolicy(): void
{
$rules = $this->getPolicyRules();

Expand Down Expand Up @@ -171,5 +164,4 @@ public function warmUp($cacheDir): array

return [];
}

}
1 change: 0 additions & 1 deletion Builder/TwigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class TwigAdapter
{

private Environment $twigEnvironment;

public function __construct(Environment $twigEnvironment)
Expand Down
4 changes: 2 additions & 2 deletions CacheWarmer/TwigSandboxCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Intaro\TwigSandboxBundle\CacheWarmer;

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

class TwigSandboxCacheWarmer implements CacheWarmerInterface
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public function warmUp($cacheDir): array
* A warmer should return true if the cache can be
* generated incrementally and on-demand.
*
* @return Boolean true if the warmer is optional, false otherwise
* @return bool true if the warmer is optional, false otherwise
*/
public function isOptional()
{
Expand Down
8 changes: 4 additions & 4 deletions DependencyInjection/IntaroTwigSandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Intaro\TwigSandboxBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration
Expand All @@ -17,12 +17,12 @@ class IntaroTwigSandboxExtension extends Extension
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}
8 changes: 4 additions & 4 deletions Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Intaro\TwigSandboxBundle\Loader;

use Doctrine\Common\Annotations\Reader;
use Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Config\Resource\FileResource;
use Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules;

class AnnotationClassLoader implements LoaderInterface
{
protected $reader;
protected $annotationClass = 'Intaro\\TwigSandboxBundle\\Annotation\\Sandbox';
protected $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)
public function setAnnotationClass($class): void
{
$this->annotationClass = $class;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public function supports($resource, $type = null): bool
/**
* {@inheritdoc}
*/
public function setResolver(LoaderResolverInterface $resolver)
public function setResolver(LoaderResolverInterface $resolver): void
{
}

Expand Down
17 changes: 8 additions & 9 deletions Loader/AnnotationFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Intaro\TwigSandboxBundle\Loader;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\FileLocator;
use Intaro\TwigSandboxBundle\SecurityPolicy\SecurityPolicyRules;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;

class AnnotationFileLoader extends FileLoader
{
Expand All @@ -14,9 +14,8 @@ class AnnotationFileLoader extends FileLoader
/**
* Constructor.
*
* @param FileLocator $locator A FileLocator instance
* @param AnnotationClassLoader $loader An AnnotationClassLoader instance
* @param string|null $env
* @param FileLocator $locator A FileLocator instance
* @param AnnotationClassLoader $loader An AnnotationClassLoader instance
*/
public function __construct(FileLocator $locator, AnnotationClassLoader $loader, string $env = null)
{
Expand Down Expand Up @@ -67,12 +66,12 @@ public function supports($resource, $type = null): bool
*
* @return string|false Full class name if found, false otherwise
*/
protected function findClass($file)
protected function findClass(string $file)
{
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
for ($i = 0, $count = count($tokens); $i < $count; $i++) {
for ($i = 0, $count = count($tokens); $i < $count; ++$i) {
$token = $tokens[$i];

if (!is_array($token)) {
Expand All @@ -88,7 +87,7 @@ protected function findClass($file)
do {
$namespace .= $token[1];
$token = $tokens[++$i];
} while ($i < $count && is_array($token) && in_array($token[0], array(T_NS_SEPARATOR, T_STRING)));
} while ($i < $count && is_array($token) && in_array($token[0], [T_NS_SEPARATOR, T_STRING]));
}

if (T_CLASS === $token[0]) {
Expand Down
8 changes: 4 additions & 4 deletions SecurityPolicy/SecurityPolicyRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getProperties()
return $this->properties;
}

public function addMethod($class, $method)
public function addMethod($class, $method): void
{
if (!isset($this->methods[$class])) {
$this->methods[$class] = [];
Expand All @@ -41,7 +41,7 @@ public function addMethod($class, $method)
}
}

public function addProperty($class, $property)
public function addProperty($class, $property): void
{
if (!isset($this->properties[$class])) {
$this->properties[$class] = [];
Expand All @@ -67,12 +67,12 @@ public function getResources()
*
* @param ResourceInterface $resource A resource instance
*/
public function addResource(ResourceInterface $resource)
public function addResource(ResourceInterface $resource): void
{
$this->resources[] = $resource;
}

public function merge(SecurityPolicyRules $rules)
public function merge(self $rules): void
{
$this->resources = array_merge($this->resources, $rules->getResources());

Expand Down
Loading

0 comments on commit 3f5df8b

Please sign in to comment.