Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClassReflector.php
Go to the documentation of this file.
1 <?php
7 
8 use Zend\Code\Reflection\MethodReflection;
9 
14 {
18  protected $_typeProcessor;
19 
25  public function __construct(\Magento\Framework\Reflection\TypeProcessor $typeProcessor)
26  {
27  $this->_typeProcessor = $typeProcessor;
28  }
29 
64  public function reflectClassMethods($className, $methods)
65  {
66  $data = [];
67  $classReflection = new \Zend\Code\Reflection\ClassReflection($className);
69  foreach ($classReflection->getMethods() as $methodReflection) {
70  $methodName = $methodReflection->getName();
71  if (array_key_exists($methodName, $methods)) {
72  $data[$methodName] = $this->extractMethodData($methodReflection);
73  }
74  }
75  return $data;
76  }
77 
85  public function extractMethodData(\Zend\Code\Reflection\MethodReflection $method)
86  {
87  $methodData = ['documentation' => $this->extractMethodDescription($method), 'interface' => []];
89  foreach ($method->getParameters() as $parameter) {
90  $parameterData = [
91  'type' => $this->_typeProcessor->register($this->_typeProcessor->getParamType($parameter)),
92  'required' => !$parameter->isOptional(),
93  'documentation' => $this->_typeProcessor->getParamDescription($parameter),
94  ];
95  if ($parameter->isOptional()) {
96  $parameterData['default'] = $parameter->getDefaultValue();
97  }
98  $methodData['interface']['in']['parameters'][$parameter->getName()] = $parameterData;
99  }
100  $returnType = $this->_typeProcessor->getGetterReturnType($method);
101  if ($returnType['type'] != 'void' && $returnType['type'] != 'null') {
102  $methodData['interface']['out']['parameters']['result'] = [
103  'type' => $this->_typeProcessor->register($returnType['type']),
104  'documentation' => $returnType['description'],
105  'required' => true,
106  ];
107  }
108  $exceptions = $this->_typeProcessor->getExceptions($method);
109  if (!empty($exceptions)) {
110  $methodData['interface']['out']['throws'] = $exceptions;
111  }
112 
113  return $methodData;
114  }
115 
122  protected function extractMethodDescription(\Zend\Code\Reflection\MethodReflection $method)
123  {
124  $methodReflection = new MethodReflection(
125  $method->getDeclaringClass()->getName(),
126  $method->getName()
127  );
128 
129  $docBlock = $methodReflection->getDocBlock();
130  if (!$docBlock) {
131  throw new \LogicException(
132  'The docBlock of the method '.
133  $method->getDeclaringClass()->getName() . '::' . $method->getName() . ' is empty.'
134  );
135  }
136  return $this->_typeProcessor->getDescription($docBlock);
137  }
138 
146  {
147  $classReflection = new \Zend\Code\Reflection\ClassReflection($className);
148  $docBlock = $classReflection->getDocBlock();
149  if (!$docBlock) {
150  return '';
151  }
152  return $this->_typeProcessor->getDescription($docBlock);
153  }
154 }
extractMethodDescription(\Zend\Code\Reflection\MethodReflection $method)
$methods
Definition: billing.phtml:71
__construct(\Magento\Framework\Reflection\TypeProcessor $typeProcessor)
$method
Definition: info.phtml:13
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31