Skip to content

Commit

Permalink
MAGETWO-51906: Refactor TypeProcessor
Browse files Browse the repository at this point in the history
 - refactor
  • Loading branch information
Cari Spruiell committed Apr 22, 2016
1 parent ebba9e7 commit 79c551b
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 70 deletions.
123 changes: 123 additions & 0 deletions lib/internal/Magento/Framework/Reflection/NameFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Reflection;

use Zend\Code\Reflection\ClassReflection;

class NameFinder
{
/**
* Convert Data Object getter name into field name.
*
* @param string $getterName
* @return string
*/
public function dataObjectGetterNameToFieldName($getterName)
{
if ((strpos($getterName, 'get') === 0)) {
/** Remove 'get' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('get'));
} elseif ((strpos($getterName, 'is') === 0)) {
/** Remove 'is' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('is'));
} elseif ((strpos($getterName, 'has') === 0)) {
/** Remove 'has' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('has'));
} else {
$fieldName = $getterName;
}
return lcfirst($fieldName);
}

/**
* Convert Data Object getter short description into field description.
*
* @param string $shortDescription
* @return string
*/
public function dataObjectGetterDescriptionToFieldDescription($shortDescription)
{
return ucfirst(substr(strstr($shortDescription, " "), 1));
}

/**
* Find the getter method name for a property from the given class
*
* @param ClassReflection $class
* @param string $camelCaseProperty
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding getter method
*/
public function findGetterMethodName(ClassReflection $class, $camelCaseProperty)
{
$getterName = 'get' . $camelCaseProperty;
$boolGetterName = 'is' . $camelCaseProperty;
return $this->findAccessorMethodName($class, $camelCaseProperty, $getterName, $boolGetterName);
}

/**
* Find the setter method name for a property from the given class
*
* @param ClassReflection $class
* @param string $camelCaseProperty
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding setter method
*/
public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
{
$setterName = 'set' . $camelCaseProperty;
$boolSetterName = 'setIs' . $camelCaseProperty;
return $this->findAccessorMethodName($class, $camelCaseProperty, $setterName, $boolSetterName);
}

/**
* Find the accessor method name for a property from the given class
*
* @param ClassReflection $class
* @param string $camelCaseProperty
* @param string $accessorName
* @param bool $boolAccessorName
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding setter method
*/
public function findAccessorMethodName(
ClassReflection $class,
$camelCaseProperty,
$accessorName,
$boolAccessorName
) {
if ($this->classHasMethod($class, $accessorName)) {
$methodName = $accessorName;
return $methodName;
} elseif ($this->classHasMethod($class, $boolAccessorName)) {
$methodName = $boolAccessorName;
return $methodName;
} else {
throw new \LogicException(
sprintf(
'Property "%s" does not have corresponding setter in class "%s".',
$camelCaseProperty,
$class->getName()
)
);
}
}

/**
* Checks if method is defined
*
* Case sensitivity of the method is taken into account.
*
* @param ClassReflection $class
* @param string $methodName
* @return bool
*/
public function classHasMethod(ClassReflection $class, $methodName)
{
return $class->hasMethod($methodName) && ($class->getMethod($methodName)->getName() == $methodName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreStart
namespace Magento\Framework\Reflection\Test\Unit;

use Zend\Code\Reflection\ClassReflection;
use Magento\Framework\Exception\SerializationException;

/**
* NameFinder Unit Test
*/
class NameFinderTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Framework\Reflection\NameFinder */
protected $nameFinder;

/**
* Set up helper.
*/
protected function setUp()
{
$this->nameFinder = new \Magento\Framework\Reflection\NameFinder();
}

public function testFindSetterMethodName()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$setterName = $this->nameFinder->findSetterMethodName($class, 'AttrName');
$this->assertEquals("setAttrName", $setterName);

$booleanSetterName = $this->nameFinder->findSetterMethodName($class, 'Active');
$this->assertEquals("setIsActive", $booleanSetterName);
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
*/
public function testFindSetterMethodNameInvalidAttribute()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$this->nameFinder->findSetterMethodName($class, 'InvalidAttribute');
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
*/
public function testFindSetterMethodNameWrongCamelCasedAttribute()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$this->nameFinder->findSetterMethodName($class, 'ActivE');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,36 +216,6 @@ public function testProcessSimpleTypeInvalidType()
$this->_typeProcessor->processSimpleAndAnyType($value, $type);
}

public function testFindSetterMethodName()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$setterName = $this->_typeProcessor->findSetterMethodName($class, 'AttrName');
$this->assertEquals("setAttrName", $setterName);

$booleanSetterName = $this->_typeProcessor->findSetterMethodName($class, 'Active');
$this->assertEquals("setIsActive", $booleanSetterName);
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
*/
public function testFindSetterMethodNameInvalidAttribute()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$this->_typeProcessor->findSetterMethodName($class, 'InvalidAttribute');
}

/**
* @expectedException \Exception
* @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/
*/
public function testFindSetterMethodNameWrongCamelCasedAttribute()
{
$class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject");
$this->_typeProcessor->findSetterMethodName($class, 'ActivE');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessageRegExp /@param annotation is incorrect for the parameter "name" \w+/
Expand Down
80 changes: 42 additions & 38 deletions lib/internal/Magento/Framework/Reflection/TypeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ class TypeProcessor
*/
protected $_types = [];

/**
* @var NameFinder
*/
private $nameFinder;

/**
* The getter function to get the new NameFinder dependency
*
* @return NameFinder
*
* @deprecated
*/
private function getNameFinder()
{
if ($this->nameFinder === null) {
$this->nameFinder = \Magento\Framework\App\ObjectManager::getInstance()
->get('\Magento\Framework\Reflection\NameFinder');
}
return $this->nameFinder;
}

/**
* Retrieve processed types data.
*
Expand Down Expand Up @@ -195,11 +216,11 @@ protected function _processMethod(\Zend\Code\Reflection\MethodReflection $method
/** Field will not be added to WSDL if getter has params */
if ($isGetter && !$methodReflection->getNumberOfRequiredParameters()) {
$returnMetadata = $this->getGetterReturnType($methodReflection);
$fieldName = $this->dataObjectGetterNameToFieldName($methodReflection->getName());
$fieldName = $this->getNameFinder()->dataObjectGetterNameToFieldName($methodReflection->getName());
if ($returnMetadata['description']) {
$description = $returnMetadata['description'];
} else {
$description = $this->dataObjectGetterDescriptionToFieldDescription(
$description = $this->getNameFinder()->dataObjectGetterDescriptionToFieldDescription(
$methodReflection->getDocBlock()->getShortDescription()
);
}
Expand Down Expand Up @@ -237,33 +258,25 @@ public function getDescription(\Zend\Code\Reflection\DocBlockReflection $doc)
*
* @param string $getterName
* @return string
*
* @deprecated
*/
public function dataObjectGetterNameToFieldName($getterName)
{
if ((strpos($getterName, 'get') === 0)) {
/** Remove 'get' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('get'));
} elseif ((strpos($getterName, 'is') === 0)) {
/** Remove 'is' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('is'));
} elseif ((strpos($getterName, 'has') === 0)) {
/** Remove 'has' prefix and make the first letter lower case */
$fieldName = substr($getterName, strlen('has'));
} else {
$fieldName = $getterName;
}
return lcfirst($fieldName);
return $this->getNameFinder()->dataObjectGetterNameToFieldName($getterName);
}

/**
* Convert Data Object getter short description into field description.
*
* @param string $shortDescription
* @return string
*
* @deprecated
*/
protected function dataObjectGetterDescriptionToFieldDescription($shortDescription)
{
return ucfirst(substr(strstr($shortDescription, " "), 1));
return $this->getNameFinder()->dataObjectGetterDescriptionToFieldDescription($shortDescription);
}

/**
Expand Down Expand Up @@ -578,12 +591,12 @@ public function getParamDescription(ParameterReflection $param)
* @param string $camelCaseProperty
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding getter method
*
* @deprecated
*/
public function findGetterMethodName(ClassReflection $class, $camelCaseProperty)
{
$getterName = 'get' . $camelCaseProperty;
$boolGetterName = 'is' . $camelCaseProperty;
return $this->findAccessorMethodName($class, $camelCaseProperty, $getterName, $boolGetterName);
return $this->getNameFinder()->findGetterMethodName($class, $camelCaseProperty);
}

/**
Expand Down Expand Up @@ -614,12 +627,12 @@ protected function setType(&$value, $type)
* @param string $camelCaseProperty
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding setter method
*
* @deprecated
*/
public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
{
$setterName = 'set' . $camelCaseProperty;
$boolSetterName = 'setIs' . $camelCaseProperty;
return $this->findAccessorMethodName($class, $camelCaseProperty, $setterName, $boolSetterName);
return $this->getNameFinder()->findSetterMethodName($class, $camelCaseProperty);
}

/**
Expand All @@ -631,28 +644,17 @@ public function findSetterMethodName(ClassReflection $class, $camelCaseProperty)
* @param bool $boolAccessorName
* @return string processed method name
* @throws \Exception If $camelCaseProperty has no corresponding setter method
*
* @deprecated
*/
protected function findAccessorMethodName(
ClassReflection $class,
$camelCaseProperty,
$accessorName,
$boolAccessorName
) {
if ($this->classHasMethod($class, $accessorName)) {
$methodName = $accessorName;
return $methodName;
} elseif ($this->classHasMethod($class, $boolAccessorName)) {
$methodName = $boolAccessorName;
return $methodName;
} else {
throw new \LogicException(
sprintf(
'Property "%s" does not have corresponding setter in class "%s".',
$camelCaseProperty,
$class->getName()
)
);
}
return $this->getNameFinder()
->findAccessorMethodName($class, $camelCaseProperty, $accessorName, $boolAccessorName);
}

/**
Expand All @@ -663,10 +665,12 @@ protected function findAccessorMethodName(
* @param ClassReflection $class
* @param string $methodName
* @return bool
*
* @deprecated
*/
protected function classHasMethod(ClassReflection $class, $methodName)
{
return $class->hasMethod($methodName) && ($class->getMethod($methodName)->getName() == $methodName);
return $this->getNameFinder()->classHasMethod($class, $methodName);
}

/**
Expand Down
Loading

0 comments on commit 79c551b

Please sign in to comment.