123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?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\Alias;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Alias\SetTypeToCastFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\SetTypeToCastFixer>
- */
- final class SetTypeToCastFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield 'null cast' => [
- '<?php $foo = null;',
- '<?php settype($foo, "null");',
- ];
- yield 'null cast comments' => [
- '<?php
- # 0
- $foo = null# 1
- # 2
- # 3
- # 4
- # 5
- # 6
- # 7
- # 8
- # 9
- # 10
- ;',
- '<?php
- # 0
- settype# 1
- # 2
- (# 3
- # 4
- $foo# 5
- # 6
- ,# 7
- # 8
- "null"# 9
- )# 10
- ;',
- ];
- yield 'array + spacing' => [
- '<?php $foo = (array) $foo;',
- '<?php settype ( $foo , \'array\');',
- ];
- yield 'bool + casing' => [
- '<?php $foo = (bool) $foo;',
- '<?php settype ( $foo , "Bool");',
- ];
- yield 'boolean with extra space' => [
- '<?php $foo = (bool) $foo;',
- '<?php settype ( $foo , "boolean");',
- ];
- yield 'double' => [
- '<?php $foo = (float) $foo;',
- '<?php settype($foo, "double");',
- ];
- yield 'float' => [
- '<?php $foo = (float) $foo;',
- '<?php settype($foo, "float");',
- ];
- yield 'float in loop' => [
- '<?php while(a()){$foo = (float) $foo;}',
- '<?php while(a()){settype($foo, "float");}',
- ];
- yield 'int full caps' => [
- '<?php $foo = (int) $foo;',
- '<?php settype($foo, "INT");',
- ];
- yield 'integer (simple)' => [
- '<?php $foo = (int) $foo;',
- '<?php settype($foo, "integer");',
- ];
- yield 'object' => [
- '<?php echo 1; $foo = (object) $foo;',
- '<?php echo 1; settype($foo, "object");',
- ];
- yield 'string' => [
- '<?php $foo = (string) $foo;',
- '<?php settype($foo, "string");',
- ];
- yield 'string in function body' => [
- '<?php function A(){ $foo = (string) $foo; return $foo; }',
- '<?php function A(){ settype($foo, "string"); return $foo; }',
- ];
- yield 'integer + no space' => [
- '<?php $foo = (int) $foo;',
- '<?php settype($foo,"integer");',
- ];
- yield 'no space comments' => [
- '<?php /*0*//*1*/$foo = (int) $foo/*2*//*3*//*4*//*5*//*6*//*7*/;/*8*/',
- '<?php /*0*//*1*/settype/*2*/(/*3*/$foo/*4*/,/*5*/"integer"/*6*/)/*7*/;/*8*/',
- ];
- yield 'comments with line breaks' => [
- '<?php #0
- #1
- $foo = (int) $foo#2
- #3
- #4
- #5
- #6
- #7
- #8
- ;#9',
- '<?php #0
- #1
- settype#2
- #3
- (#4
- $foo#5
- ,#6
- "integer"#7
- )#8
- ;#9',
- ];
- // do not fix cases
- yield 'first argument is not a variable' => [
- '<?php
- namespace A\B; // comment
- function settype($a, $b){} // "
- settype(1, "double");
- ',
- ];
- yield 'first argument is variable followed by operation' => [
- '<?php
- namespace A\B; // comment
- function settype($a, $b){} // "
- settype($foo + 1, "integer"); // function must be overridden, so do not fix it
- ',
- ];
- yield 'wrong numbers of arguments' => [
- '<?php settype($foo, "integer", $a);',
- ];
- yield 'other namespace I' => [
- '<?php a\b\settype($foo, "integer", $a);',
- ];
- yield 'other namespace II' => [
- '<?php \b\settype($foo, "integer", $a);',
- ];
- yield 'static call' => [
- '<?php A::settype($foo, "integer");',
- ];
- yield 'member call' => [
- '<?php $a->settype($foo, "integer");',
- ];
- yield 'unknown type' => [
- '<?php $a->settype($foo, "foo");',
- ];
- yield 'return value used I' => [
- '<?php $a = settype($foo, "integer");',
- ];
- yield 'return value used II' => [
- '<?php a(settype($foo, "integer"));',
- ];
- yield 'return value used III' => [
- '<?php $a = "123"; $b = [settype($a, "integer")];',
- ];
- yield 'return value used IV' => [
- '<?php $a = "123"; $b = [3 => settype($a, "integer")];',
- ];
- yield 'return value used V' => [
- '<?= settype($foo, "object");',
- ];
- yield 'wrapped statements, fixable after removing the useless parenthesis brace' => [
- '<?php
- settype(/*1*//*2*/($a), \'int\');
- settype($b, (\'int\'));
- settype(($c), (\'int\'));
- settype((($d)), ((\'int\')));
- ',
- ];
- yield 'wrapped statements, not-fixable, even after removing the useless parenthesis brace' => [
- '<?php
- namespace A\B; // comment
- function settype($a, $b){} // "
- settype($foo1, (("integer")."1"));
- settype($foo2, ("1".("integer")));
- settype($foo3, ((("integer")."1")));
- settype((($foo)+1), "integer");
- ',
- ];
- yield 'nested is not an issue for this fixer, as these non may be fixed' => [
- '<?php
- settype($foo, settype($foo, "double"));
- settype(settype($foo, "double"), "double");
- ',
- ];
- yield 'unknown type II' => [
- '<?php settype($foo, "stringX");',
- ];
- yield 'boolean' => [
- '<?php $foo = (bool) $foo;',
- '<?php settype($foo, "boolean", );',
- ];
- yield 'comments with line breaks II' => [
- '<?php #0
- #1
- $foo = (int) $foo#2
- #3
- #4
- #5
- #6
- #7
- #8
- #9
- ;#10',
- '<?php #0
- #1
- settype#2
- #3
- (#4
- $foo#5
- ,#6
- "integer"#7
- ,#8
- )#9
- ;#10',
- ];
- }
- }
|