public static function getClientVarsFromNewKeyword()

in src/Fixers/ClientUpgradeFixer/ClientVar.php [114:155]


    public static function getClientVarsFromNewKeyword(Tokens $tokens, array $clientShortNames): array
    {
        $clientVars = [];
        foreach ($tokens as $index => $token) {
            // get variables which are set directly
            if (!$token->isGivenKind(T_NEW)) {
                continue;
            }

            $nextToken = $tokens[$tokens->getNextMeaningfulToken($index)];
            $shortName = $nextToken->getContent();
            if (!in_array($shortName, $clientShortNames)) {
                continue;
            }

            if (!$prevIndex = $tokens->getPrevMeaningfulToken($index)) {
                continue;
            }

            if ($tokens[$prevIndex]->getContent() !== '=') {
                continue;
            }

            if (!$prevIndex = $tokens->getPrevMeaningfulToken($prevIndex)) {
                continue;
            }

            if (
                $tokens[$prevIndex]->isGivenKind(T_VARIABLE) || (
                    $tokens[$prevIndex]->isGivenKind(T_STRING)
                    && $tokens[$prevIndex - 1]->isGivenKind(T_OBJECT_OPERATOR)
                )
            ) {
                // Handle clients set to $var
                $clientClass = array_search($shortName, $clientShortNames);
                $varName = $tokens[$prevIndex]->getContent();
                $clientVars[$varName] = new ClientVar($varName, $clientClass);
            }
        }

        return $clientVars;
    }