Skip to content

Commit

Permalink
update cs-fixer, add same rules we have on the distribution, and fix …
Browse files Browse the repository at this point in the history
…the project
  • Loading branch information
JoMessina committed Oct 20, 2023
1 parent 8f84b2e commit 2b5aff6
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
- uses: actions/checkout@v2
- name: Cs-Fixer
run: |
wget -q https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer
wget -q https://cs.symfony.com/download/php-cs-fixer-v3.phar -O php-cs-fixer
chmod a+x php-cs-fixer
PHP_CS_FIXER_IGNORE_ENV=true ./php-cs-fixer fix src --dry-run
./php-cs-fixer fix src --dry-run
phpstan:
runs-on: ubuntu-latest
Expand Down
41 changes: 41 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in('src')
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@PHP82Migration' => true,
'@PHP81Migration' => true,
'@PHP80Migration:risky' => true,
'@PHPUnit84Migration:risky' => true,
'@PSR1' => true,
'@PSR12' => true,
'@PhpCsFixer' => true,
'@Symfony' => true,
'ternary_to_elvis_operator' => true,
'set_type_to_cast' => true,
'self_accessor' => true,
'psr_autoloading' => true,
'php_unit_test_annotation' => ['style' => 'annotation'],
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_construct' => true,
'no_useless_sprintf' => true,
'no_homoglyph_names' => true,
'native_function_invocation' => true,
'native_constant_invocation' => true,
'modernize_types_casting' => true,
'logical_operators' => true,
'is_null' => true,
'function_to_constant' => true,
'fopen_flag_order' => true,
'error_suppression' => true,
'ereg_to_preg' => true,
'dir_constant' => true,
'method_chaining_indentation' => false,
])
->setFinder($finder)
->setCacheFile('.php-cs-fixer.cache') // forward compatibility with 3.x line
;
6 changes: 4 additions & 2 deletions src/BillingAddress.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand All @@ -18,14 +20,14 @@ public function __construct($name)
private function compile(string $addresses): string
{
return <<<PHP
(array_values(array_filter($addresses, fn (object \$item) => \$item->getDefaultBilling() === true))[0] ?? null)
(array_values(array_filter({$addresses}, fn (object \$item) => \$item->getDefaultBilling() === true))[0] ?? null)
PHP;
}

private function evaluate(array $context, array $addresses): ?array
{
return array_values(
array_filter($addresses, fn (object $item) => $item->getDefaultBilling() === true)
array_filter($addresses, fn (object $item) => true === $item->getDefaultBilling())
)[0] ?? null;
}
}
4 changes: 3 additions & 1 deletion src/CustomAttribute.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand All @@ -18,7 +20,7 @@ public function __construct($name)
private function compile(string $attributeCode): string
{
return <<<PHP
(array_values(array_filter(\$input->getCustomAttributes(), fn (\$item) => \$item->getAttributeCode() === $attributeCode))[0]->getValue() ?? null)
(array_values(array_filter(\$input->getCustomAttributes(), fn (\$item) => \$item->getAttributeCode() === {$attributeCode}))[0]->getValue() ?? null)
PHP;
}

Expand Down
4 changes: 3 additions & 1 deletion src/FilterCustomAttribute.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand All @@ -18,7 +20,7 @@ public function __construct($name)
private function compile(string $attributeCode): string
{
return <<<PHP
(fn (\$item) => \$item->getAttributeCode() === $attributeCode)
(fn (\$item) => \$item->getAttributeCode() === {$attributeCode})
PHP;
}

Expand Down
20 changes: 10 additions & 10 deletions src/Normalize.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Kiboko\Component\ExpressionLanguage\Magento\Serializable\ProductCatalogSerializableRejectData;
use Kiboko\Component\ExpressionLanguage\Magento\Serializable\SerializableRejectDataInterface;
use ReflectionObject;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;

class Normalize extends ExpressionFunction
Expand All @@ -21,7 +20,7 @@ public function __construct($name)
private function compile(string $input, int $depth = 2): string
{
return <<<PHP
(function() use ($input){
(function() use ({$input}){
\$serializer = function(\$input, int \$depth = 2) use (&\$serializer) {
if (\$depth === 0 || !is_array(\$input) && !is_object(\$input)) {
return json_encode(\$input);
Expand All @@ -41,30 +40,31 @@ private function compile(string $input, int $depth = 2): string
}
return \$output;
};
return \$serializer(\$input, $depth);
return \$serializer(\$input, {$depth});
})()
PHP;
}

private function evaluate(array $context, mixed $input, int $depth = 2): array
{
$serializer = function ($input, int $depth = 2) use(&$serializer) {
if ($depth === 0 || !is_array($input) && !is_object($input)) {
$serializer = function ($input, int $depth = 2) use (&$serializer) {
if (0 === $depth || !\is_array($input) && !\is_object($input)) {
return json_encode($input);
}
$output = [];
if (is_object($input)) {
$object = new ReflectionObject($input);
if (\is_object($input)) {
$object = new \ReflectionObject($input);
foreach ($object->getProperties() as $property) {
$value = $property->getValue($input);
$output[$property->getName()] = $serializer($value, $depth - 1);
}
}
if (is_array($input)) {
if (\is_array($input)) {
foreach ($input as $key => $value) {
$output[$key] = $serializer($value, $depth - 1);
}
}

return $output;
};

Expand Down
6 changes: 4 additions & 2 deletions src/OrderSimpleItems.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand All @@ -18,12 +20,12 @@ public function __construct($name)
private function compile(string $items): string
{
return <<<PHP
(array_filter($items, fn (object \$item) => \$item->getProductType() === 'simple'))
(array_filter({$items}, fn (object \$item) => \$item->getProductType() === 'simple'))
PHP;
}

private function evaluate(array $context, array $items): array
{
return array_filter($items, fn (object $item) => $item->getProductType() === 'simple');
return array_filter($items, fn (object $item) => 'simple' === $item->getProductType());
}
}
6 changes: 4 additions & 2 deletions src/ShippingAddress.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand All @@ -18,14 +20,14 @@ public function __construct($name)
private function compile(string $addresses): string
{
return <<<PHP
(array_values(array_filter($addresses, fn (object \$item) => \$item->getDefaultShipping() === true))[0] ?? null)
(array_values(array_filter({$addresses}, fn (object \$item) => \$item->getDefaultShipping() === true))[0] ?? null)
PHP;
}

private function evaluate(array $context, array $addresses): ?array
{
return array_values(
array_filter($addresses, fn (object $item) => $item->getDefaultShipping() === true)
array_filter($addresses, fn (object $item) => true === $item->getDefaultShipping())
)[0] ?? null;
}
}
2 changes: 2 additions & 0 deletions src/Street.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\ExpressionLanguage\Magento;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
Expand Down

0 comments on commit 2b5aff6

Please sign in to comment.