InterfaceValidator.php 8 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Interception\Code;

use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Phrase;

class InterfaceValidator
{
    const METHOD_BEFORE = 'before';

    const METHOD_AROUND = 'around';

    const METHOD_AFTER = 'after';

    /**
     * Arguments reader model
     *
     * @var \Magento\Framework\Code\Reader\ArgumentsReader
     */
    protected $_argumentsReader;

    /**
     * @param \Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader
     */
    public function __construct(\Magento\Framework\Code\Reader\ArgumentsReader $argumentsReader = null)
    {
        $this->_argumentsReader = $argumentsReader ?: new \Magento\Framework\Code\Reader\ArgumentsReader();
    }

    /**
     * Validate plugin interface
     *
     * @param string $pluginClass
     * @param string $interceptedType
     *
     * @return void
     * @throws ValidatorException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
     */
    public function validate($pluginClass, $interceptedType)
    {
        $interceptedType = '\\' . trim($interceptedType, '\\');
        $pluginClass = '\\' . trim($pluginClass, '\\');
        $plugin = new \ReflectionClass($pluginClass);
        $type = new \ReflectionClass($interceptedType);

        foreach ($plugin->getMethods(\ReflectionMethod::IS_PUBLIC) as $pluginMethod) {
            /** @var  $pluginMethod \ReflectionMethod */
            $originMethodName = $this->getOriginMethodName($pluginMethod->getName());
            if ($originMethodName === null) {
                continue;
            }
            if (!$type->hasMethod($originMethodName)) {
                throw new ValidatorException(
                    new Phrase(
                        'Incorrect interface in %1. There is no method [ %2 ] in %3 interface',
                        [$pluginClass, $originMethodName, $interceptedType]
                    )
                );
            }
            $originMethod = $type->getMethod($originMethodName);

            $pluginMethodParameters = $this->getMethodParameters($pluginMethod);
            $originMethodParameters = $this->getMethodParameters($originMethod);

            $methodType = $this->getMethodType($pluginMethod->getName());

            $subject = array_shift($pluginMethodParameters);
            if (!$this->_argumentsReader->isCompatibleType(
                $subject['type'],
                $interceptedType
            ) || $subject['type'] === null
            ) {
                throw new ValidatorException(
                    new Phrase(
                        'Invalid [%1] $%2 type in %3::%4. It must be compatible with %5',
                        [$subject['type'], $subject['name'], $pluginClass, $pluginMethod->getName(), $interceptedType]
                    )
                );
            }

            switch ($methodType) {
                case self::METHOD_BEFORE:
                    $this->validateMethodsParameters(
                        $pluginMethodParameters,
                        $originMethodParameters,
                        $pluginClass,
                        $pluginMethod->getName()
                    );
                    break;
                case self::METHOD_AROUND:
                    $proceed = array_shift($pluginMethodParameters);
                    if (!$this->_argumentsReader->isCompatibleType($proceed['type'], '\\Closure')) {
                        throw new ValidatorException(
                            new Phrase(
                                'Invalid [%1] $%2 type in %3::%4. It must be compatible with \\Closure',
                                [$proceed['type'], $proceed['name'], $pluginClass, $pluginMethod->getName()]
                            )
                        );
                    }
                    $this->validateMethodsParameters(
                        $pluginMethodParameters,
                        $originMethodParameters,
                        $pluginClass,
                        $pluginMethod->getName()
                    );
                    break;
                case self::METHOD_AFTER:
                    if (count($pluginMethodParameters) > 1) {
                        // remove result
                        array_shift($pluginMethodParameters);
                        $matchedParameters = array_intersect_key($originMethodParameters, $pluginMethodParameters);
                        $this->validateMethodsParameters(
                            $pluginMethodParameters,
                            $matchedParameters,
                            $pluginClass,
                            $pluginMethod->getName()
                        );
                    }
                    break;
            }
        }
    }

    /**
     * Validate methods parameters compatibility
     *
     * @param array $pluginParameters
     * @param array $originParameters
     * @param string $class
     * @param string $method
     *
     * @return void
     * @throws ValidatorException
     */
    protected function validateMethodsParameters(array $pluginParameters, array $originParameters, $class, $method)
    {
        if (count($pluginParameters) != count($originParameters)) {
            throw new ValidatorException(
                new Phrase(
                    'Invalid method signature. Invalid method parameters count in %1::%2',
                    [$class, $method]
                )
            );
        }
        foreach ($pluginParameters as $position => $data) {
            if (!$this->_argumentsReader->isCompatibleType($data['type'], $originParameters[$position]['type'])) {
                throw new ValidatorException(
                    new Phrase(
                        'Incompatible parameter type [%1 $%2] in %3::%4. It must be compatible with %5',
                        [$data['type'], $data['name'], $class, $method, $originParameters[$position]['type']]
                    )
                );
            }
        }
    }

    /**
     * Get parameters type
     *
     * @param \ReflectionParameter $parameter
     *
     * @return string
     */
    protected function getParametersType(\ReflectionParameter $parameter)
    {
        $parameterClass = $parameter->getClass();
        $type = $parameterClass ? '\\' . $parameterClass->getName() : ($parameter->isArray() ? 'array' : null);
        return $type;
    }

    /**
     * Get intercepted method name
     *
     * @param string $pluginMethodName
     *
     * @return string|null
     */
    protected function getOriginMethodName($pluginMethodName)
    {
        switch ($this->getMethodType($pluginMethodName)) {
            case self::METHOD_BEFORE:
            case self::METHOD_AROUND:
                return lcfirst(substr($pluginMethodName, 6));

            case self::METHOD_AFTER:
                return lcfirst(substr($pluginMethodName, 5));

            default:
                return null;
        }
    }

    /**
     * Get method type
     *
     * @param string $pluginMethodName
     *
     * @return null|string
     */
    protected function getMethodType($pluginMethodName)
    {
        if (substr($pluginMethodName, 0, 6) == self::METHOD_BEFORE) {
            return self::METHOD_BEFORE;
        } elseif (substr($pluginMethodName, 0, 6) == self::METHOD_AROUND) {
            return self::METHOD_AROUND;
        } elseif (substr($pluginMethodName, 0, 5) == self::METHOD_AFTER) {
            return self::METHOD_AFTER;
        }

        return null;
    }

    /**
     * Get method parameters
     *
     * @param \ReflectionMethod $method
     *
     * @return array
     */
    protected function getMethodParameters(\ReflectionMethod $method)
    {
        $output = [];
        foreach ($method->getParameters() as $parameter) {
            $output[$parameter->getPosition()] = [
                'name' => $parameter->getName(),
                'type' => $this->getParametersType($parameter),
            ];
        }
        return $output;
    }
}