-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-51906: Refactor TypeProcessor
- refactor
- Loading branch information
Cari Spruiell
committed
Apr 22, 2016
1 parent
ebba9e7
commit 79c551b
Showing
5 changed files
with
245 additions
and
70 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
lib/internal/Magento/Framework/Reflection/NameFinder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
lib/internal/Magento/Framework/Reflection/Test/Unit/NameFinderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.