Skip to content

Closure::bind, $this problem. #14

@ilsenem

Description

@ilsenem

Error:

Access to an undefined property Some\Class\With\CallbackStackProcessing::$counter

Sample abstract code:

/**
 * Storage.php
 */

class Storage
{
    
    private $data;

    public function __get($key)
    {
        return $this->data[$key] ?? null;
    }

    public function __set($key, $value)
    {
        $this->data[$key] = $value;
    }

}

/**
 * Inside Some\Class\With\CallbackStackProcessing::process()
 */

$storage = new Storage;
$result  = $this->data;

foreach ($this->processors as $processor)
{
    $result = Closure::bind($processor, $storage)($result);
}

return $result;

/**
 * Callbacks in Some\Class\With\CallbackStackProcessing::$processors
 */

[
    function ($data) {
        $this->counter = count($data);
        
        return $data;
    },
    function ($data) {
        return array_map($data, function ($item) {
            return (int) $data;
        });
    },
    function ($data) {
        if ($data[0] < 100) return false;

        return $data;
    },
    function ($data) {
        return [
            'before' => $this->counter,
            'items'  => $data,
            'after'  => count($data),
        ];
    },
]

Is there any way to tell analyzer to skip this particular error or typehint via annotations?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions