Skip to content

Commit 3d0ecc4

Browse files
authored
Merge pull request #3799 from samsonasik/add-rector-rule-to-pass-third-parameter-true-to-in-array
Add new Custom Rector Rule to Pass true to 3rd parameter of in_array when no value provided
2 parents b6a75cf + 077bbb5 commit 3d0ecc4

10 files changed

Lines changed: 92 additions & 7 deletions

File tree

app/Config/Mimes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,14 @@ public static function guessExtensionFromType(string $type, string $proposedExte
507507

508508
$proposedExtension = trim(strtolower($proposedExtension));
509509

510-
if ($proposedExtension !== '' && array_key_exists($proposedExtension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposedExtension]) ? [static::$mimes[$proposedExtension]] : static::$mimes[$proposedExtension]))
510+
if ($proposedExtension !== '' && array_key_exists($proposedExtension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposedExtension]) ? [static::$mimes[$proposedExtension]] : static::$mimes[$proposedExtension], true))
511511
{
512512
return $proposedExtension;
513513
}
514514

515515
foreach (static::$mimes as $ext => $types)
516516
{
517-
if ((is_string($types) && $types === $type) || (is_array($types) && in_array($type, $types)))
517+
if ((is_string($types) && $types === $type) || (is_array($types) && in_array($type, $types, true)))
518518
{
519519
return $ext;
520520
}

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ parameters:
4545
- '#Return type \(bool\) of method CodeIgniter\\HTTP\\Files\\UploadedFile::move\(\) should be compatible with return type \(CodeIgniter\\Files\\File\) of method CodeIgniter\\Files\\File::move\(\)#'
4646
- '#Return type \(bool\) of method CodeIgniter\\Test\\TestLogger::log\(\) should be compatible with return type \(null\) of method Psr\\Log\\LoggerInterface::log\(\)#'
4747
- '#Unsafe usage of new static\(\)*#'
48+
- '#Access to an undefined property CodeIgniter\\Config\\BaseConfig::\$key#'
4849
parallel:
4950
processTimeout: 300.0
5051
scanDirectories:

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector;
55
use Rector\SOLID\Rector\If_\RemoveAlwaysElseRector;
66
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
7+
use Utils\Rector\PassStrictParameterToFunctionParameterRector;
78
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;
89

910
return static function (ContainerConfigurator $containerConfigurator): void {
@@ -40,4 +41,5 @@
4041
$services->set(UnderscoreToCamelCaseVariableNameRector::class);
4142
$services->set(SimplifyUselessVariableRector::class);
4243
$services->set(RemoveAlwaysElseRector::class);
44+
$services->set(PassStrictParameterToFunctionParameterRector::class);
4345
};

system/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ function helper($filenames)
662662
}
663663

664664
// Check if this helper has already been loaded
665-
if (in_array($filename, $loaded))
665+
if (in_array($filename, $loaded, true))
666666
{
667667
continue;
668668
}

system/Config/Services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public static function email($config = null, bool $getShared = true)
224224
return static::getSharedInstance('email', $config);
225225
}
226226

227+
// @phpstan-ignore-next-line
227228
if (empty($config) || ! (is_array($config) || $config instanceof EmailConfig))
228229
{
229230
$config = config('Email');

system/Database/Sqlsrv/Forge.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ protected function _processForeignKeys(string $table): string
301301
. ' FOREIGN KEY (' . $this->db->escapeIdentifiers($field) . ') '
302302
. ' REFERENCES ' . $this->db->escapeIdentifiers($this->db->getPrefix() . $fkey['table']) . ' (' . $this->db->escapeIdentifiers($fkey['field']) . ')';
303303

304-
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions))
304+
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $allowActions, true))
305305
{
306306
$sql .= ' ON DELETE ' . $fkey['onDelete'];
307307
}
308308

309-
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions))
309+
if ($fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $allowActions, true))
310310
{
311311
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
312312
}

system/Router/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ private function checkSubdomains($subdomains): bool
15141514

15151515
// Routes can be limited to any sub-domain. In that case, though,
15161516
// it does require a sub-domain to be present.
1517-
if (! empty($this->currentSubdomain) && in_array('*', $subdomains))
1517+
if (! empty($this->currentSubdomain) && in_array('*', $subdomains, true))
15181518
{
15191519
return true;
15201520
}

system/Validation/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected function processRules(string $field, string $label = null, $value, $ru
249249
$rules = array_diff($rules, ['if_exist']);
250250
}
251251

252-
if (in_array('permit_empty', $rules))
252+
if (in_array('permit_empty', $rules, true))
253253
{
254254
if (! in_array('required', $rules, true) && (is_array($value) ? empty($value) : (trim($value) === '')))
255255
{

system/View/Table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ public function makeColumns($array = [], $columnLimit = 0)
224224
}
225225
while (count($array) > 0);
226226

227+
// @phpstan-ignore-next-line
227228
return $new;
228229
}
229230

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Utils\Rector;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\ConstFetch;
10+
use PhpParser\Node\Expr\FuncCall;
11+
use PhpParser\Node\Name;
12+
use Rector\Core\Rector\AbstractRector;
13+
use Rector\Core\RectorDefinition\CodeSample;
14+
use Rector\Core\RectorDefinition\RectorDefinition;
15+
16+
/**
17+
* Pass strict to function parameter on specific position argument when no value provided
18+
*/
19+
final class PassStrictParameterToFunctionParameterRector extends AbstractRector
20+
{
21+
private const FUNCTION_WITH_ARG_POSITION = [
22+
// position start from 0
23+
'in_array' => 2,
24+
];
25+
26+
public function getDefinition(): RectorDefinition
27+
{
28+
return new RectorDefinition('Pass strict to function parameter on specific position argument when no value provided', [
29+
new CodeSample(
30+
<<<'CODE_SAMPLE'
31+
in_array('a', $array);
32+
CODE_SAMPLE
33+
,
34+
<<<'CODE_SAMPLE'
35+
in_array('a', $array, true);
36+
CODE_SAMPLE
37+
),
38+
]);
39+
}
40+
41+
/**
42+
* @return string[]
43+
*/
44+
public function getNodeTypes(): array
45+
{
46+
return [FuncCall::class];
47+
}
48+
49+
/**
50+
* @param FuncCall $node
51+
*/
52+
public function refactor(Node $node): ?Node
53+
{
54+
$name = $node->name;
55+
if (! method_exists($name, 'toString'))
56+
{
57+
return null;
58+
}
59+
60+
$functions = array_keys(self::FUNCTION_WITH_ARG_POSITION);
61+
$currentFunctionName = $name->toString();
62+
63+
if (! in_array($currentFunctionName, $functions, true))
64+
{
65+
return null;
66+
}
67+
68+
$position = self::FUNCTION_WITH_ARG_POSITION[$currentFunctionName];
69+
70+
if (isset($node->args[$position]))
71+
{
72+
return null;
73+
}
74+
75+
$name = new Name('true');
76+
$node->args[$position] = new Arg(new ConstFetch($name));
77+
78+
return $node;
79+
}
80+
}

0 commit comments

Comments
 (0)