123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
- use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Andreas Möller <am@localheinz.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer
- */
- final class NativeFunctionInvocationFixerTest extends AbstractFixerTestCase
- {
- public function testConfigureRejectsUnknownConfigurationKey(): void
- {
- $key = 'foo';
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessage(\sprintf(
- '[native_function_invocation] Invalid configuration: The option "%s" does not exist.',
- $key
- ));
- $this->fixer->configure([
- $key => 'bar',
- ]);
- }
- /**
- * @dataProvider provideConfigureRejectsInvalidConfigurationElementCases
- *
- * @param mixed $element
- */
- public function testConfigureRejectsInvalidConfigurationElement($element, string $expectedExceptionMessage): void
- {
- $this->expectException(InvalidConfigurationException::class);
- $this->expectExceptionMessage($expectedExceptionMessage);
- $this->fixer->configure([
- 'exclude' => [$element],
- ]);
- }
- public static function provideConfigureRejectsInvalidConfigurationElementCases(): iterable
- {
- yield 'null' => [
- null,
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
- ];
- yield 'false' => [
- false,
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
- ];
- yield 'true' => [
- true,
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
- ];
- yield 'int' => [
- 1,
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
- ];
- yield 'array' => [
- [],
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "array".',
- ];
- yield 'float' => [
- 0.1,
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "float".',
- ];
- yield 'object' => [
- new \stdClass(),
- '[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "stdClass".',
- ];
- yield 'not-trimmed' => [
- ' is_string ',
- '[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration['include'] $include
- * @param class-string<\Throwable> $expectedExceptionClass
- *
- * @dataProvider provideConfigureIncludeSetsCases
- */
- public function testConfigureIncludeSets(
- array $include,
- ?string $expectedExceptionClass = null,
- ?string $expectedExceptionMessage = null
- ): void {
- if (null !== $expectedExceptionClass) {
- $this->expectException($expectedExceptionClass);
- $this->expectExceptionMessageMatches(\sprintf('#^%s$#', preg_quote($expectedExceptionMessage, '#')));
- }
- $this->fixer->configure([
- 'include' => $include,
- ]);
- if (null === $expectedExceptionClass) {
- $this->addToAssertionCount(1);
- }
- }
- public static function provideConfigureIncludeSetsCases(): iterable
- {
- yield [['foo', 'bar']];
- yield [[NativeFunctionInvocationFixer::SET_ALL]];
- yield [[NativeFunctionInvocationFixer::SET_ALL, 'bar']];
- yield [
- ['@xxx'],
- InvalidFixerConfigurationException::class,
- '[native_function_invocation] Invalid configuration: Unknown set "@xxx", known sets are "@all", "@internal" and "@compiler_optimized".',
- ];
- yield [
- [' x '],
- InvalidFixerConfigurationException::class,
- '[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
- ];
- }
- public function testConfigureResetsExclude(): void
- {
- $this->fixer->configure([
- 'exclude' => [
- 'is_string',
- ],
- ]);
- $before = <<<'PHP'
- <?php
- namespace WithClassNotPrefixed;
- class Bar
- {
- public function baz($foo)
- {
- if (isset($foo)) {
- is_string($foo);
- }
- }
- }
- PHP;
- $after = <<<'PHP'
- <?php
- namespace WithClassNotPrefixed;
- class Bar
- {
- public function baz($foo)
- {
- if (isset($foo)) {
- \is_string($foo);
- }
- }
- }
- PHP;
- $this->doTest($before);
- $this->fixer->configure([]);
- $this->doTest($after, $before);
- }
- /**
- * @dataProvider provideFixWithDefaultConfigurationCases
- */
- public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixWithDefaultConfigurationCases(): iterable
- {
- yield [
- '<?php
- \is_string($foo);
- ',
- ];
- yield [
- '<?php
- \is_string($foo);
- ',
- '<?php
- is_string($foo);
- ',
- ];
- yield [
- '<?php
- class Foo
- {
- public function bar($foo)
- {
- return \is_string($foo);
- }
- }
- ',
- ];
- yield [
- '<?php
- json_encode($foo);
- \strlen($foo);
- ',
- '<?php
- json_encode($foo);
- strlen($foo);
- ',
- ];
- yield [
- '<?php
- class Foo
- {
- public function bar($foo)
- {
- return \IS_STRING($foo);
- }
- }
- ',
- '<?php
- class Foo
- {
- public function bar($foo)
- {
- return IS_STRING($foo);
- }
- }
- ',
- ];
- yield 'fix multiple calls in single code' => [
- '<?php
- json_encode($foo);
- \strlen($foo);
- \strlen($foo);
- ',
- '<?php
- json_encode($foo);
- strlen($foo);
- strlen($foo);
- ',
- ];
- yield [
- '<?php $name = \get_class($foo, );',
- '<?php $name = get_class($foo, );',
- ];
- }
- /**
- * @dataProvider provideFixWithConfiguredExcludeCases
- */
- public function testFixWithConfiguredExclude(string $expected, ?string $input = null): void
- {
- $this->fixer->configure([
- 'exclude' => [
- 'is_string',
- ],
- ]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideFixWithConfiguredExcludeCases(): iterable
- {
- yield [
- '<?php
- is_string($foo);
- ',
- ];
- yield [
- '<?php
- class Foo
- {
- public function bar($foo)
- {
- return is_string($foo);
- }
- }
- ',
- ];
- }
- /**
- * @dataProvider provideFixWithNamespaceConfigurationCases
- *
- * @param _AutogeneratedInputConfiguration['scope'] $scope
- */
- public function testFixWithNamespaceConfiguration(string $expected, ?string $input = null, string $scope = 'namespaced'): void
- {
- $this->fixer->configure(['scope' => $scope]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string, 2?: string}>
- */
- public static function provideFixWithNamespaceConfigurationCases(): iterable
- {
- yield [
- '<?php echo count([1]);',
- ];
- yield [
- '<?php
- namespace space1 { ?>
- <?php echo \count([2]) ?>
- <?php }namespace {echo count([1]);}
- ',
- '<?php
- namespace space1 { ?>
- <?php echo count([2]) ?>
- <?php }namespace {echo count([1]);}
- ',
- ];
- yield [
- '<?php
- namespace Bar {
- echo \strLEN("in 1");
- }
- namespace {
- echo strlen("out 1");
- }
- namespace {
- echo strlen("out 2");
- }
- namespace Bar{
- echo \strlen("in 2");
- }
- namespace {
- echo strlen("out 3");
- }
- ',
- '<?php
- namespace Bar {
- echo strLEN("in 1");
- }
- namespace {
- echo strlen("out 1");
- }
- namespace {
- echo strlen("out 2");
- }
- namespace Bar{
- echo strlen("in 2");
- }
- namespace {
- echo strlen("out 3");
- }
- ',
- ];
- yield [
- '<?php
- namespace space11 ?>
- <?php
- echo \strlen(__NAMESPACE__);
- namespace space2;
- echo \strlen(__NAMESPACE__);
- ',
- '<?php
- namespace space11 ?>
- <?php
- echo strlen(__NAMESPACE__);
- namespace space2;
- echo strlen(__NAMESPACE__);
- ',
- ];
- yield [
- '<?php namespace PhpCsFixer\Tests\Fixer\Casing;\count([1]);',
- '<?php namespace PhpCsFixer\Tests\Fixer\Casing;count([1]);',
- ];
- yield [
- '<?php
- namespace Space12;
- echo \count([1]);
- namespace Space2;
- echo \count([1]);
- ?>
- ',
- '<?php
- namespace Space12;
- echo count([1]);
- namespace Space2;
- echo count([1]);
- ?>
- ',
- ];
- yield [
- '<?php namespace {echo strlen("out 2");}',
- ];
- yield [
- '<?php
- namespace space13 {
- echo \strlen("in 1");
- }
- namespace space2 {
- echo \strlen("in 2");
- }
- namespace { // global
- echo strlen("global 1");
- }
- ',
- '<?php
- namespace space13 {
- echo strlen("in 1");
- }
- namespace space2 {
- echo strlen("in 2");
- }
- namespace { // global
- echo strlen("global 1");
- }
- ',
- ];
- yield [
- '<?php
- namespace space1 {
- echo \count([1]);
- }
- namespace {
- echo \count([1]);
- }
- ',
- '<?php
- namespace space1 {
- echo count([1]);
- }
- namespace {
- echo count([1]);
- }
- ',
- 'all',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixWithConfiguredIncludeCases
- */
- public function testFixWithConfiguredInclude(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideFixWithConfiguredIncludeCases(): iterable
- {
- yield 'include set + 1, exclude 1' => [
- '<?php
- echo \count([1]);
- \some_other($a, 3);
- echo strlen($a);
- not_me();
- ',
- '<?php
- echo count([1]);
- some_other($a, 3);
- echo strlen($a);
- not_me();
- ',
- [
- 'include' => [NativeFunctionInvocationFixer::SET_INTERNAL, 'some_other'],
- 'exclude' => ['strlen'],
- ],
- ];
- yield 'include @all' => [
- '<?php
- echo \count([1]);
- \some_other($a, 3);
- echo \strlen($a);
- \me_as_well();
- ',
- '<?php
- echo count([1]);
- some_other($a, 3);
- echo strlen($a);
- me_as_well();
- ',
- [
- 'include' => [NativeFunctionInvocationFixer::SET_ALL],
- ],
- ];
- yield 'include @compiler_optimized' => [
- '<?php
- // do not fix
- $a = strrev($a);
- $a .= str_repeat($a, 4);
- $b = already_prefixed_function();
- // fix
- $c = \get_class($d);
- $e = \intval($f);
- ',
- '<?php
- // do not fix
- $a = strrev($a);
- $a .= str_repeat($a, 4);
- $b = \already_prefixed_function();
- // fix
- $c = get_class($d);
- $e = intval($f);
- ',
- [
- 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
- ],
- ];
- yield [
- '<?php class Foo {
- public function & strlen($name) {
- }
- }
- ',
- ];
- yield 'scope namespaced and strict enabled' => [
- '<?php
- $a = not_compiler_optimized_function();
- $b = intval($c);
- ',
- '<?php
- $a = \not_compiler_optimized_function();
- $b = \intval($c);
- ',
- [
- 'scope' => 'namespaced',
- 'strict' => true,
- ],
- ];
- yield [
- '<?php
- use function foo\json_decode;
- json_decode($base);
- ',
- null,
- [
- 'include' => [NativeFunctionInvocationFixer::SET_ALL],
- ],
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, 1?: string, 2?: array<string, mixed>}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield 'include @compiler_optimized with strict enabled' => [
- '<?php
- $a = not_compiler_optimized_function();
- $b = not_compiler_optimized_function();
- $c = \intval($d);
- ',
- '<?php
- $a = \not_compiler_optimized_function();
- $b = \ not_compiler_optimized_function();
- $c = intval($d);
- ',
- [
- 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
- 'strict' => true,
- ],
- ];
- yield [
- '<?php
- echo \/**/strlen($a);
- echo \ strlen($a);
- echo \#
- #
- strlen($a);
- echo \strlen($a);
- ',
- '<?php
- echo \/**/strlen($a);
- echo \ strlen($a);
- echo \#
- #
- strlen($a);
- echo strlen($a);
- ',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null, array $config = []): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideFix80Cases(): iterable
- {
- yield 'attribute and strict' => [
- '<?php
- #[\Attribute(\Attribute::TARGET_CLASS)]
- class Foo {}
- ',
- null,
- ['strict' => true],
- ];
- yield 'null safe operator' => ['<?php $x?->count();'];
- yield 'multiple function-calls-like in attribute' => [
- '<?php
- #[Foo(), Bar(), Baz()]
- class Foo {}
- ',
- null,
- ['include' => ['@all']],
- ];
- }
- }
|