Skip to content

Commit 79c551b

Browse files
author
Cari Spruiell
committed
MAGETWO-51906: Refactor TypeProcessor
- refactor
1 parent ebba9e7 commit 79c551b

File tree

5 files changed

+245
-70
lines changed

5 files changed

+245
-70
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Reflection;
8+
9+
use Zend\Code\Reflection\ClassReflection;
10+
11+
class NameFinder
12+
{
13+
/**
14+
* Convert Data Object getter name into field name.
15+
*
16+
* @param string $getterName
17+
* @return string
18+
*/
19+
public function dataObjectGetterNameToFieldName($getterName)
20+
{
21+
if ((strpos($getterName, 'get') === 0)) {
22+
/** Remove 'get' prefix and make the first letter lower case */
23+
$fieldName = substr($getterName, strlen('get'));
24+
} elseif ((strpos($getterName, 'is') === 0)) {
25+
/** Remove 'is' prefix and make the first letter lower case */
26+
$fieldName = substr($getterName, strlen('is'));
27+
} elseif ((strpos($getterName, 'has') === 0)) {
28+
/** Remove 'has' prefix and make the first letter lower case */
29+
$fieldName = substr($getterName, strlen('has'));
30+
} else {
31+
$fieldName = $getterName;
32+
}
33+
return lcfirst($fieldName);
34+
}
35+
36+
/**
37+
* Convert Data Object getter short description into field description.
38+
*
39+
* @param string $shortDescription
40+
* @return string
41+
*/
42+
public function dataObjectGetterDescriptionToFieldDescription($shortDescription)
43+
{
44+
return ucfirst(substr(strstr($shortDescription, " "), 1));
45+
}
46+
47+
/**
48+
* Find the getter method name for a property from the given class
49+
*
50+
* @param ClassReflection $class
51+
* @param string $camelCaseProperty
52+
* @return string processed method name
53+
* @throws \Exception If $camelCaseProperty has no corresponding getter method
54+
*/
55+
public function findGetterMethodName(ClassReflection $class, $camelCaseProperty)
56+
{
57+
$getterName = 'get' . $camelCaseProperty;
58+
$boolGetterName = 'is' . $camelCaseProperty;
59+
return $this->findAccessorMethodName($class, $camelCaseProperty, $getterName, $boolGetterName);
60+
}
61+
62+
/**
63+
* Find the setter method name for a property from the given class
64+
*
65+
* @param ClassReflection $class
66+
* @param string $camelCaseProperty
67+
* @return string processed method name
68+
* @throws \Exception If $camelCaseProperty has no corresponding setter method
69+
*/
70+
public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
71+
{
72+
$setterName = 'set' . $camelCaseProperty;
73+
$boolSetterName = 'setIs' . $camelCaseProperty;
74+
return $this->findAccessorMethodName($class, $camelCaseProperty, $setterName, $boolSetterName);
75+
}
76+
77+
/**
78+
* Find the accessor method name for a property from the given class
79+
*
80+
* @param ClassReflection $class
81+
* @param string $camelCaseProperty
82+
* @param string $accessorName
83+
* @param bool $boolAccessorName
84+
* @return string processed method name
85+
* @throws \Exception If $camelCaseProperty has no corresponding setter method
86+
*/
87+
public function findAccessorMethodName(
88+
ClassReflection $class,
89+
$camelCaseProperty,
90+
$accessorName,
91+
$boolAccessorName
92+
) {
93+
if ($this->classHasMethod($class, $accessorName)) {
94+
$methodName = $accessorName;
95+
return $methodName;
96+
} elseif ($this->classHasMethod($class, $boolAccessorName)) {
97+
$methodName = $boolAccessorName;
98+
return $methodName;
99+
} else {
100+
throw new \LogicException(
101+
sprintf(
102+
'Property "%s" does not have corresponding setter in class "%s".',
103+
$camelCaseProperty,
104+
$class->getName()
105+
)
106+
);
107+
}
108+
}
109+
110+
/**
111+
* Checks if method is defined
112+
*
113+
* Case sensitivity of the method is taken into account.
114+
*
115+
* @param ClassReflection $class
116+
* @param string $methodName
117+
* @return bool
118+
*/
119+
public function classHasMethod(ClassReflection $class, $methodName)
120+
{
121+
return $class->hasMethod($methodName) && ($class->getMethod($methodName)->getName() == $methodName);
122+
}
123+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// @codingStandardsIgnoreStart
7+
namespace Magento\Framework\Reflection\Test\Unit;
8+
9+
use Zend\Code\Reflection\ClassReflection;
10+
use Magento\Framework\Exception\SerializationException;
11+
12+
/**
13+
* NameFinder Unit Test
14+
*/
15+
class NameFinderTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/** @var \Magento\Framework\Reflection\NameFinder */
18+
protected $nameFinder;
19+
20+
/**
21+
* Set up helper.
22+
*/
23+
protected function setUp()
24+
{
25+
$this->nameFinder = new \Magento\Framework\Reflection\NameFinder();
26+
}
27+
28+
public function testFindSetterMethodName()
29+
{
30+
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
31+
$setterName = $this->nameFinder->findSetterMethodName($class, 'AttrName');
32+
$this->assertEquals("setAttrName", $setterName);
33+
34+
$booleanSetterName = $this->nameFinder->findSetterMethodName($class, 'Active');
35+
$this->assertEquals("setIsActive", $booleanSetterName);
36+
}
37+
38+
/**
39+
* @expectedException \Exception
40+
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
41+
*/
42+
public function testFindSetterMethodNameInvalidAttribute()
43+
{
44+
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
45+
$this->nameFinder->findSetterMethodName($class, 'InvalidAttribute');
46+
}
47+
48+
/**
49+
* @expectedException \Exception
50+
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
51+
*/
52+
public function testFindSetterMethodNameWrongCamelCasedAttribute()
53+
{
54+
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
55+
$this->nameFinder->findSetterMethodName($class, 'ActivE');
56+
}
57+
}

lib/internal/Magento/Framework/Reflection/Test/Unit/TypeProcessorTest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -216,36 +216,6 @@ public function testProcessSimpleTypeInvalidType()
216216
$this->_typeProcessor->processSimpleAndAnyType($value, $type);
217217
}
218218

219-
public function testFindSetterMethodName()
220-
{
221-
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
222-
$setterName = $this->_typeProcessor->findSetterMethodName($class, 'AttrName');
223-
$this->assertEquals("setAttrName", $setterName);
224-
225-
$booleanSetterName = $this->_typeProcessor->findSetterMethodName($class, 'Active');
226-
$this->assertEquals("setIsActive", $booleanSetterName);
227-
}
228-
229-
/**
230-
* @expectedException \Exception
231-
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
232-
*/
233-
public function testFindSetterMethodNameInvalidAttribute()
234-
{
235-
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
236-
$this->_typeProcessor->findSetterMethodName($class, 'InvalidAttribute');
237-
}
238-
239-
/**
240-
* @expectedException \Exception
241-
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
242-
*/
243-
public function testFindSetterMethodNameWrongCamelCasedAttribute()
244-
{
245-
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
246-
$this->_typeProcessor->findSetterMethodName($class, 'ActivE');
247-
}
248-
249219
/**
250220
* @expectedException \LogicException
251221
* @expectedExceptionMessageRegExp /@param annotation is incorrect for the parameter "name" \w+/

lib/internal/Magento/Framework/Reflection/TypeProcessor.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ class TypeProcessor
6060
*/
6161
protected $_types = [];
6262

63+
/**
64+
* @var NameFinder
65+
*/
66+
private $nameFinder;
67+
68+
/**
69+
* The getter function to get the new NameFinder dependency
70+
*
71+
* @return NameFinder
72+
*
73+
* @deprecated
74+
*/
75+
private function getNameFinder()
76+
{
77+
if ($this->nameFinder === null) {
78+
$this->nameFinder = \Magento\Framework\App\ObjectManager::getInstance()
79+
->get('\Magento\Framework\Reflection\NameFinder');
80+
}
81+
return $this->nameFinder;
82+
}
83+
6384
/**
6485
* Retrieve processed types data.
6586
*
@@ -195,11 +216,11 @@ protected function _processMethod(\Zend\Code\Reflection\MethodReflection $method
195216
/** Field will not be added to WSDL if getter has params */
196217
if ($isGetter && !$methodReflection->getNumberOfRequiredParameters()) {
197218
$returnMetadata = $this->getGetterReturnType($methodReflection);
198-
$fieldName = $this->dataObjectGetterNameToFieldName($methodReflection->getName());
219+
$fieldName = $this->getNameFinder()->dataObjectGetterNameToFieldName($methodReflection->getName());
199220
if ($returnMetadata['description']) {
200221
$description = $returnMetadata['description'];
201222
} else {
202-
$description = $this->dataObjectGetterDescriptionToFieldDescription(
223+
$description = $this->getNameFinder()->dataObjectGetterDescriptionToFieldDescription(
203224
$methodReflection->getDocBlock()->getShortDescription()
204225
);
205226
}
@@ -237,33 +258,25 @@ public function getDescription(\Zend\Code\Reflection\DocBlockReflection $doc)
237258
*
238259
* @param string $getterName
239260
* @return string
261+
*
262+
* @deprecated
240263
*/
241264
public function dataObjectGetterNameToFieldName($getterName)
242265
{
243-
if ((strpos($getterName, 'get') === 0)) {
244-
/** Remove 'get' prefix and make the first letter lower case */
245-
$fieldName = substr($getterName, strlen('get'));
246-
} elseif ((strpos($getterName, 'is') === 0)) {
247-
/** Remove 'is' prefix and make the first letter lower case */
248-
$fieldName = substr($getterName, strlen('is'));
249-
} elseif ((strpos($getterName, 'has') === 0)) {
250-
/** Remove 'has' prefix and make the first letter lower case */
251-
$fieldName = substr($getterName, strlen('has'));
252-
} else {
253-
$fieldName = $getterName;
254-
}
255-
return lcfirst($fieldName);
266+
return $this->getNameFinder()->dataObjectGetterNameToFieldName($getterName);
256267
}
257268

258269
/**
259270
* Convert Data Object getter short description into field description.
260271
*
261272
* @param string $shortDescription
262273
* @return string
274+
*
275+
* @deprecated
263276
*/
264277
protected function dataObjectGetterDescriptionToFieldDescription($shortDescription)
265278
{
266-
return ucfirst(substr(strstr($shortDescription, " "), 1));
279+
return $this->getNameFinder()->dataObjectGetterDescriptionToFieldDescription($shortDescription);
267280
}
268281

269282
/**
@@ -578,12 +591,12 @@ public function getParamDescription(ParameterReflection $param)
578591
* @param string $camelCaseProperty
579592
* @return string processed method name
580593
* @throws \Exception If $camelCaseProperty has no corresponding getter method
594+
*
595+
* @deprecated
581596
*/
582597
public function findGetterMethodName(ClassReflection $class, $camelCaseProperty)
583598
{
584-
$getterName = 'get' . $camelCaseProperty;
585-
$boolGetterName = 'is' . $camelCaseProperty;
586-
return $this->findAccessorMethodName($class, $camelCaseProperty, $getterName, $boolGetterName);
599+
return $this->getNameFinder()->findGetterMethodName($class, $camelCaseProperty);
587600
}
588601

589602
/**
@@ -614,12 +627,12 @@ protected function setType(&$value, $type)
614627
* @param string $camelCaseProperty
615628
* @return string processed method name
616629
* @throws \Exception If $camelCaseProperty has no corresponding setter method
630+
*
631+
* @deprecated
617632
*/
618633
public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
619634
{
620-
$setterName = 'set' . $camelCaseProperty;
621-
$boolSetterName = 'setIs' . $camelCaseProperty;
622-
return $this->findAccessorMethodName($class, $camelCaseProperty, $setterName, $boolSetterName);
635+
return $this->getNameFinder()->findSetterMethodName($class, $camelCaseProperty);
623636
}
624637

625638
/**
@@ -631,28 +644,17 @@ public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
631644
* @param bool $boolAccessorName
632645
* @return string processed method name
633646
* @throws \Exception If $camelCaseProperty has no corresponding setter method
647+
*
648+
* @deprecated
634649
*/
635650
protected function findAccessorMethodName(
636651
ClassReflection $class,
637652
$camelCaseProperty,
638653
$accessorName,
639654
$boolAccessorName
640655
) {
641-
if ($this->classHasMethod($class, $accessorName)) {
642-
$methodName = $accessorName;
643-
return $methodName;
644-
} elseif ($this->classHasMethod($class, $boolAccessorName)) {
645-
$methodName = $boolAccessorName;
646-
return $methodName;
647-
} else {
648-
throw new \LogicException(
649-
sprintf(
650-
'Property "%s" does not have corresponding setter in class "%s".',
651-
$camelCaseProperty,
652-
$class->getName()
653-
)
654-
);
655-
}
656+
return $this->getNameFinder()
657+
->findAccessorMethodName($class, $camelCaseProperty, $accessorName, $boolAccessorName);
656658
}
657659

658660
/**
@@ -663,10 +665,12 @@ protected function findAccessorMethodName(
663665
* @param ClassReflection $class
664666
* @param string $methodName
665667
* @return bool
668+
*
669+
* @deprecated
666670
*/
667671
protected function classHasMethod(ClassReflection $class, $methodName)
668672
{
669-
return $class->hasMethod($methodName) && ($class->getMethod($methodName)->getName() == $methodName);
673+
return $this->getNameFinder()->classHasMethod($class, $methodName);
670674
}
671675

672676
/**

0 commit comments

Comments
 (0)