Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with overwrite of existing preset config options #111

Merged
merged 1 commit into from May 20, 2019

Conversation

@dol
Copy link
Contributor

commented May 20, 2019

Q A
Bug fix? yes

Currently it's not possible to overwrite an existing config from the preset. The following example will produce the following error:

<?php declare(strict_types=1);

use SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff;

return [
    'preset' => 'default',
    'config' => [
        DeclareStrictTypesSniff::class => [
            'newlinesCountBetweenOpenTagAndDeclare' => 0,
        ],
    ],
];

PHP Notice: Array to string conversion in <path>/phpinsights/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/SniffSettingsHelper.php on line 21
The reason for this problem is that array_merge_recursive merges two values into an array instead of replacing it. array_replace_recursive will replace the setting instead of creating an array out of the two values 1 and 0.

Test code:

<?php declare(strict_types=1);

$config = [
    \SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff::class => [
        'newlinesCountBetweenOpenTagAndDeclare' => 0,
    ],
];
print_r(array_merge_recursive(\NunoMaduro\PhpInsights\Application\DefaultPreset::get()['config'], $config));
print_r(array_replace_recursive(\NunoMaduro\PhpInsights\Application\DefaultPreset::get()['config'], $config));

Expeceted after the fix:

Array
(
    [SlevomatCodingStandard\Sniffs\Commenting\DocCommentSpacingSniff] => Array
        (
            [linesCountBetweenDifferentAnnotationsTypes] => 1
        )

    [SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff] => Array
        (
            [newlinesCountBetweenOpenTagAndDeclare] => 0
            [spacesCountAroundEqualsSign] => 0
        )

)

Actual:

Array
(
    [SlevomatCodingStandard\Sniffs\Commenting\DocCommentSpacingSniff] => Array
        (
            [linesCountBetweenDifferentAnnotationsTypes] => 1
        )

    [SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff] => Array
        (
            [newlinesCountBetweenOpenTagAndDeclare] => Array
                (
                    [0] => 2
                    [1] => 0
                )

            [spacesCountAroundEqualsSign] => 0
        )

)

@nunomaduro nunomaduro merged commit 0567bab into nunomaduro:master May 20, 2019

1 check failed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
@nunomaduro

This comment has been minimized.

Copy link
Owner

commented May 20, 2019

Thanks for this!

@dol

This comment has been minimized.

Copy link
Contributor Author

commented May 20, 2019

Your welcome. Nice project. Sweet spot of good default settings and usability. Well done.

@dol dol deleted the dol:fix/array_merge_recursive branch May 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.