Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ 7.4 ]
php: [ 7.2, 7.3, 7.4 ]
os: [ ubuntu-latest ]
steps:
- name: Checkout
Expand All @@ -27,3 +27,9 @@ jobs:
uses: php-actions/phpspec@master
with:
config: phpspec.yml.dist

- name: Lint code
run: vendor/bin/php-cs-fixer fix -v --dry-run && vendor/bin/rector process --dry-run

- name: Static analysis
run: vendor/bin/phpstan analyse --ansi --memory-limit=0
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ The configuration (`config/packages/ckrack_optimus.yaml`) looks as follows:

```yaml
ckrack_optimus:
# Set options, as documented at https://github.com/jenssegers/optimus#usage
prime: "%env(int:OPTIMUS_PRIME)%"
inverse: "%env(int:OPTIMUS_INVERSE)%"
random: "%env(int:OPTIMUS_RANDOM)%"

# Large prime number lower than 2147483647
prime: %env(int:OPTIMUS_PRIME)%

# The inverse prime so that (PRIME * INVERSE) & MAXID == 1
inverse: %env(int:OPTIMUS_INVERSE)%

# A large random integer lower than 2147483647
random: %env(int:OPTIMUS_RANDOM)%

# if set to true, param converter will continue with the next available
# if set to true, param converter will continue with the next available param converters
passthrough: true

```

To generate the env variables, we can use optimus' `spark` command.
Expand Down
21 changes: 19 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@
}
},
"require": {
"php": ">=7.1",
"php": ">=7.2",
"jenssegers/optimus": "^1.1",
"sensio/framework-extra-bundle": "~3.0",
"symfony/http-kernel": "^4.0 || ^5.0"
},
"require-dev": {
"bossa/phpspec2-expect": "^3.0",
"phpspec/phpspec": "^6.1",
"ergebnis/phpstan-rules": "^0.15.0",
"friendsofphp/php-cs-fixer": "^2.16.3",
"phpspec/phpspec": "^4.3 || ^5.1 || ^6.1",
"phpstan/phpstan-strict-rules": "^0.12.2",
"phpstan/phpstan": "^0.12.31",
"rector/rector": "^0.7.37",
"roave/security-advisories": "dev-master",
"thecodingmachine/phpstan-strict-rules": "^0.12.0",
"twig/twig": "^2.7"
},
"suggest": {
Expand All @@ -42,5 +48,16 @@
"*": "dist"
},
"sort-packages": true
},
"scripts": {
"lint:rector": "vendor/bin/rector process --dry-run",
"lint:php-cs-fixer": "vendor/bin/php-cs-fixer fix -v --dry-run",
"lint:phpstan": "vendor/bin/phpstan analyse --ansi --memory-limit=0",
"lint": [
"composer lint:rector",
"composer lint:php-cs-fixer",
"composer lint:phpstan"
],
"test": "vendor/bin/phpspec run --ansi"
}
}
29 changes: 29 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/ergebnis/phpstan-rules/rules.neon
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

parameters:
level: max
paths:
- src

checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: true
ergebnis:
classesAllowedToBeExtended:
- Symfony\Component\HttpKernel\Bundle\Bundle
- Symfony\Component\HttpKernel\DependencyInjection\Extension
- Twig\Extension\AbstractExtension
- PhpSpec\ObjectBehavior
ignoreErrors:
# We actually configure a container here
-
message: '#.*has a parameter \$container with a type declaration of .*ContainerBuilder.*#m'
path: src/DependencyInjection/CkrackOptimusExtension.php

# The DependencyInjection returns are very complex to deal with
-
message: '#Cannot call method.* on .*NodeParentInterface\|null.*#'
path: src/DependencyInjection/Configuration.php
21 changes: 21 additions & 0 deletions rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# rector.yaml
parameters:
paths:
- src
- spec
sets:
- 'action-injection-to-constructor-injection'
- 'array-str-functions-to-static-call'
- 'celebrity'
- 'doctrine'
- 'phpstan'
- 'phpunit-code-quality'
- 'solid'
- 'early-return'
- 'doctrine-code-quality'
- 'code-quality'
- 'php71'
- 'php72'
- 'php73'
exclude_rectors:
- Rector\SOLID\Rector\Class_\RepeatedLiteralToClassConstantRector
2 changes: 1 addition & 1 deletion spec/ParamConverter/OptimusParamConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;

class OptimusParamConverterSpec extends ObjectBehavior
final class OptimusParamConverterSpec extends ObjectBehavior
{
public function let()
{
Expand Down
2 changes: 1 addition & 1 deletion spec/Twig/OptimusExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class OptimusExtensionSpec extends ObjectBehavior
final class OptimusExtensionSpec extends ObjectBehavior
{
public function it_is_initializable(Optimus $optimus)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CkrackOptimusBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CkrackOptimusBundle extends Bundle
final class CkrackOptimusBundle extends Bundle
{
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/CkrackOptimusExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class CkrackOptimusExtension extends Extension
final class CkrackOptimusExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
Expand Down
27 changes: 14 additions & 13 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace Ckrack\OptimusBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeParentInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): NodeParentInterface
{
Expand All @@ -17,19 +18,19 @@ public function getConfigTreeBuilder(): NodeParentInterface
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
->integerNode('prime')
/*
* use the lowest possible prime, because optimus must instantiate with valid numbers.
* they will be replaced by configuration provided by the recipe.
*/
->defaultValue(3)
->info('Large prime number lower than 2147483647')
->end()
->children()
->integerNode('prime')
/*
* Use the lowest possible prime, because optimus must instantiate with valid numbers.
* They will be replaced by configuration provided by the recipe.
*/
->defaultValue(3)
->info('Large prime number lower than 2147483647')
->end()
->integerNode('inverse')
->defaultValue(715827883)
->info('The inverse prime so that (PRIME * INVERSE) & MAXID == 1')
->end()
->defaultValue(715827883)
->info('The inverse prime so that (PRIME * INVERSE) & MAXID == 1')
->end()
->integerNode('random')
->defaultValue(1592469642)
->info('A large random integer lower than 2147483647')
Expand Down
54 changes: 29 additions & 25 deletions src/ParamConverter/OptimusParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;

class OptimusParamConverter implements ParamConverterInterface
final class OptimusParamConverter implements ParamConverterInterface
{
/**
* @var Optimus
Expand All @@ -19,7 +19,12 @@ class OptimusParamConverter implements ParamConverterInterface
/**
* @var bool
*/
private $passthrough;
private $passthrough = false;

/**
* @var string
*/
private const DEFAULT_IDENTIFIER = 'optimus';

public function __construct(Optimus $optimus, bool $passthrough)
{
Expand All @@ -43,49 +48,48 @@ public function supports(ParamConverter $configuration): bool

private function setOptimus(Request $request, ParamConverter $configuration): void
{
$name = $configuration->getName();
if (!$this->hasIdentifier($request, array_replace([self::DEFAULT_IDENTIFIER => null], $configuration->getOptions()))) {
return;
}

$identifier = $this->getIdentifier(
$request,
array_replace(['optimus' => null], $configuration->getOptions()),
(string) $configuration->getName()
array_replace([self::DEFAULT_IDENTIFIER => null], $configuration->getOptions())
);

if (!$identifier) {
return;
}

if ($name == null && $identifier == 'optimus') {
$name = 'optimus';
$name = $configuration->getName();
if ($name == null && $identifier == self::DEFAULT_IDENTIFIER) {
$name = self::DEFAULT_IDENTIFIER;
}

try {
$optimus = $this->optimus->decode((int) $request->attributes->get($identifier));
$request->attributes->set($name, $optimus);
} catch (\Throwable $th) {
return;
}
$optimus = $this->optimus->decode((int) $request->attributes->get($identifier));
$request->attributes->set($name, $optimus);
}

private function getIdentifier(Request $request, $options, string $name): ?string
private function getIdentifier(Request $request, array $options): string
{
if ($options['optimus'] && !\is_array($options['optimus'])) {
return $options['optimus'];
if (\array_key_exists(self::DEFAULT_IDENTIFIER, $options) && \is_string($options[self::DEFAULT_IDENTIFIER])) {
return $options[self::DEFAULT_IDENTIFIER];
}

if ($request->attributes->has('optimus')) {
return 'optimus';
return self::DEFAULT_IDENTIFIER;
}

private function hasIdentifier(Request $request, array $options): bool
{
if (\array_key_exists(self::DEFAULT_IDENTIFIER, $options) && \is_string($options[self::DEFAULT_IDENTIFIER])) {
return true;
}

return null;
return $request->attributes->has(self::DEFAULT_IDENTIFIER);
}

private function removeOptimusOption(ParamConverter $configuration): void
{
$options = $configuration->getOptions();

if (isset($options['optimus'])) {
unset($options['optimus']);
if (\array_key_exists(self::DEFAULT_IDENTIFIER, $options)) {
unset($options[self::DEFAULT_IDENTIFIER]);
$configuration->setOptions($options);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Twig/OptimusExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class OptimusExtension extends AbstractExtension
final class OptimusExtension extends AbstractExtension
{
/**
* @var Optimus
Expand All @@ -27,7 +27,7 @@ public function getFilters(): array
];
}

public function optimusEncode($number): int
public function optimusEncode(int $number): int
{
return $this->optimus->encode($number);
}
Expand Down
Loading