kalessil / phpinspectionsea

A Static Code Analyzer for PHP (a PhpStorm/Idea Plugin)
https://plugins.jetbrains.com/plugin/7622?pr=phpStorm
Other
1.45k stars 118 forks source link

Slow array function used in loop: research recursive functions support #777

Open kalessil opened 6 years ago

kalessil commented 6 years ago

https://3v4l.org/cNpt9

<?php
class Foo {
    public $parent;
    public function __construct($parent = null) {
        $this->parent = $parent;
    }
}

$a = new Foo();
$b = new Foo($a);
$c = new Foo($b);

$x = [1, 2, 3];

/*
 * In cases like this you can do a simplified analysis:
 * - Detect if a function contains BOTH array_merge and a recursive call
 * - Detect if the return value is derived from the array_merge method
 */
function flatHash(Foo $x) {
    $hash = [spl_object_hash($x)];

    if (!$x->parent) {
        return $hash;
    }

    return array_merge($hash, flatHash($x->parent));
}

var_dump(flatHash($c));
var_dump(flatHash($b));
var_dump(flatHash($a));
stale[bot] commented 5 years ago

Bebo beep, the StaleBot is here. For one year nothing have happened here. It would be great if someone looked into details here within next 21 days when I'll close it.

andreasschroth commented 5 years ago

Sounds like a good idea to me, should be kept open. @stalebot

andreasschroth commented 5 years ago

Please reopen.

kalessil commented 5 years ago

Reopening

romanstingler commented 3 weeks ago

array merge vs array unpack php8.1+ https://3v4l.org/1PS65#v8.3.13