SourceArgumentsReader.php 3.66 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Code\Reader;

class SourceArgumentsReader
{
    /**
     * Namespace separator
     * @deprecated
     * @see \Magento\Framework\Code\Reader\NamespaceResolver::NS_SEPARATOR
     */
    const NS_SEPARATOR = '\\';

    /**
     * @var NamespaceResolver
     */
    private $namespaceResolver;

    /**
     * @param NamespaceResolver|null $namespaceResolver
     */
    public function __construct(NamespaceResolver $namespaceResolver = null)
    {
        $this->namespaceResolver = $namespaceResolver ?: new NamespaceResolver();
    }

    /**
     * Read constructor argument types from source code and perform namespace resolution if required.
     *
     * @param \ReflectionClass $class
     * @param bool $inherited
     * @return array List of constructor argument types.
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     */
    public function getConstructorArgumentTypes(
        \ReflectionClass $class,
        $inherited = false
    ) {
        $output = [null];
        if (!$class->getFileName() || false == $class->hasMethod(
            '__construct'
        ) || !$inherited && $class->getConstructor()->class !== $class->getName()
        ) {
            return $output;
        }

        //Reading parameters' types.
        $params = $class->getConstructor()->getParameters();
        /** @var string[] $types */
        $types = [];
        foreach ($params as $param) {
            //For the sake of backward compatibility.
            $typeName = '';
            if ($param->isArray()) {
                //For the sake of backward compatibility.
                $typeName = 'array';
            } else {
                try {
                    $paramClass = $param->getClass();
                    if ($paramClass) {
                        $typeName = '\\' .$paramClass->getName();
                    }
                } catch (\ReflectionException $exception) {
                    //If there's a problem loading a class then ignore it and
                    //just return it's name.
                    $typeName = '\\' .$param->getType()->getName();
                }
            }
            $types[] = $typeName;
        }
        if (!$types) {
            //For the sake of backward compatibility.
            $types = [null];
        }

        return $types;
    }

    /**
     * Perform namespace resolution if required and return fully qualified name.
     *
     * @param string $argument
     * @param array $availableNamespaces
     * @return string
     * @deprecated 101.0.0
     * @see getConstructorArgumentTypes
     */
    protected function resolveNamespaces($argument, $availableNamespaces)
    {
        return $this->namespaceResolver->resolveNamespace($argument, $availableNamespaces);
    }

    /**
     * Remove default value from argument.
     *
     * @param string $argument
     * @param string $token
     * @return string
     *
     * @deprecated 102.0.0 Not used anymore.
     */
    protected function removeToken($argument, $token)
    {
        $position = strpos($argument, $token);
        if (is_numeric($position)) {
            return substr($argument, 0, $position);
        }
        return $argument;
    }

    /**
     * Get all imported namespaces.
     *
     * @param array $file
     * @return array
     * @deprecated 101.0.0
     * @see getConstructorArgumentTypes
     */
    protected function getImportedNamespaces(array $file)
    {
        return $this->namespaceResolver->getImportedNamespaces($file);
    }
}