deprecated-packages / symplify

[DISCONTINUED] Check split packages in their own repositories :)
MIT License
618 stars 188 forks source link

"PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer" failed: Illegal offset type in file #4568

Closed notdefine closed 12 months ago

notdefine commented 12 months ago

Result of vendor/bin/ecs --debug

 [ERROR] System error: "Fixing of "/files/src/Service/Search/Query/NewsQueryBuilder.php" file by                        
         "PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer" failed: Illegal offset type in file                    
         /files/vendor/symplify/easy-coding-standard/vendor/friendsofphp/php-cs-fixer/src/Fixer/Whitespace/StatementInde
         ntationFixer.php on line 238"Run ECS with "--debug" option and post the report here: https://github.com/symplify/symplify/issues/new in
         /files/src/Service/Search/Query/NewsQueryBuilder.php:159                                                       

(!) This error only shows up on the first run. The second succeeds.

<?php

declare(strict_types=1);

namespace Mms\Api\Service\Search\Query;

use Mms\Api\Entity\Search\MmsSearchUserEntity;

class NewsQueryBuilder implements QueryBuilderInterface
{
    private $query = [];
    private $user;

    public function getQuery(): array
    {
        $query = [
            'bool' => $this->query,
        ];

        $this->reset();
        return $query;
    }

    public function buildMust(): void
    {
        if (!$this->getUser()->isSuperuser()) {
            $this->buildNormalUserMust();
        }
    }

    public function buildShould(): void
    {
        $this->query[static::QUERY_SHOULD] = [
            [
                'match' => [
                    'type.keyword' => 'News',
                ],
            ],
        ];
        $this->query['minimum_should_match'] = 1;
    }

    public function buildMustNot(): void
    {
        // TODO: Implement buildMustNot() method.
    }

    public function setUser(MmsSearchUserEntity $user): void
    {
        $this->user = $user;
    }

    public function getUser(): MmsSearchUserEntity
    {
        return $this->user;
    }

    public function reset(): void
    {
        $this->query = [];
    }

    private function buildNormalUserMust(): void
    {
        $this->query[static::QUERY_FILTER] = [
            'bool' => [
                static::QUERY_MUST => [
                    [
                        'script' => [
                            'script' => [
                                'lang' => 'painless',
                                'source' => "
                                boolean hasUserRights = true;
                                boolean hasDealerRights = false;

                                if(!doc['accessRules.dealerNo.keyword'].contains('none')) {
                                    if(doc['accessRules.dealerNo.keyword'].contains(params.dealerNo)) {
                                        return true; 
                                    }

                                    return false;
                                }

                                for(element in doc['accessRules.contractIds.keyword']) { 
                                    if(params.contractIds.contains(element)) { 
                                        hasDealerRights = true; 
                                    }
                                }

                                for(element in doc['accessRules.districtIds.keyword']) { 
                                    if(params.districtIds.contains(element)) { 
                                        hasDealerRights = true; 
                                    }
                                }

                                return (hasUserRights && hasDealerRights);
                                ",
                                'params' => [
                                    'dealerNo' => $this->getUser()->getDealerNumber(),
                                    'contractIds' => $this->getUser()->getContractIds(),
                                    'districtIds' => $this->getUser()->getDistrictIds(),
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];

        $this->query[static::QUERY_MUST] = [
            [
                'match' => [
                    'accessRules.isActive' => true,
                ],
            ],
        ];
    }
}

ecs.php

<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer;
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer;
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
use PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer;
use PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ECSConfig): void {
    $ECSConfig->paths([
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ]);

    // import SetList here on purpose to avoid overridden by existing Skip Option in current config
    $ECSConfig->sets(
        [SetList::PSR_12, SetList::SYMPLIFY]
    );

    // https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/phpdoc/phpdoc_line_span.rst
    $ECSConfig->ruleWithConfiguration(
        PhpdocLineSpanFixer::class,
        [
            'const' => 'single',
            'property' => 'single',
        ]
    );

    $ECSConfig->lineEnding("\n");

    $ECSConfig->skip([
        NotOperatorWithSuccessorSpaceFixer::class,
        CastSpacesFixer::class,
        BinaryOperatorSpacesFixer::class,
        UnaryOperatorSpacesFixer::class,
        FunctionDeclarationFixer::class
    ]);
};
samsonasik commented 12 months ago

Looking at the rule, it seems using PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer which ECS consume, the issue is probably due to latest php-cs-fixer release for that rule.

You may need to report the bug on https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues

TomasVotruba commented 12 months ago

Closing as responsibility of PHP CS Fixer.

Wirone commented 11 months ago

Looking at the rule, it seems using PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer which ECS consume, the issue is probably due to latest php-cs-fixer release for that rule.

I couldn't reproduce the issue on our side. Probably it's some kind of glitch related to @notdefine's setup. The /files/* path is interesting - is it executed in some kind of encapsulated runtime, like devcontainers or something?