Skip to content

Commit a6c162f

Browse files
committed
Add new Custom Rector Rule to Pass true to 3rd parameter of in_array when no value provided
1 parent a771a10 commit a6c162f

7 files changed

Lines changed: 79 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
}

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\PassTrueToThirdParameterInArrayRector;
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(PassTrueToThirdParameterInArrayRector::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/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
{
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 true to 3rd parameter of in_array when no value provided
18+
*/
19+
final class PassTrueToThirdParameterInArrayRector extends AbstractRector
20+
{
21+
public function getDefinition(): RectorDefinition
22+
{
23+
return new RectorDefinition('Pass true to 3rd parameter of in_array if no value provided', [
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
26+
in_array('a', $array);
27+
CODE_SAMPLE
28+
,
29+
<<<'CODE_SAMPLE'
30+
in_array('a', $array, true);
31+
CODE_SAMPLE
32+
),
33+
]);
34+
}
35+
36+
/**
37+
* @return string[]
38+
*/
39+
public function getNodeTypes(): array
40+
{
41+
return [FuncCall::class];
42+
}
43+
44+
/**
45+
* @param FuncCall $node
46+
*/
47+
public function refactor(Node $node): ?Node
48+
{
49+
$name = $node->name;
50+
if (! method_exists($name, 'toString'))
51+
{
52+
return null;
53+
}
54+
55+
if ($name->toString() !== 'in_array')
56+
{
57+
return null;
58+
}
59+
60+
if (isset($node->args[2]))
61+
{
62+
return null;
63+
}
64+
65+
$name = new Name('true');
66+
$node->args[2] = new Arg(new ConstFetch($name));
67+
68+
return $node;
69+
}
70+
}

0 commit comments

Comments
 (0)