Skip to content

Commit

Permalink
ENGCOM-4086: Optimize snail_case replacement to PascalCase #20596
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaschenko authored Feb 24, 2019
2 parents b15af15 + d5dca69 commit 371d67b
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;

class GridOnly extends \Magento\Catalog\Controller\Adminhtml\Product
use Magento\Framework\App\Action\HttpGetActionInterface;

/**
* Get specified tab grid controller.
*/
class GridOnly extends \Magento\Catalog\Controller\Adminhtml\Product implements HttpGetActionInterface
{
/**
* @var \Magento\Framework\Controller\Result\RawFactory
Expand Down Expand Up @@ -47,7 +51,7 @@ public function execute()
$this->productBuilder->build($this->getRequest());

$block = $this->getRequest()->getParam('gridOnlyBlock');
$blockClassSuffix = str_replace(' ', '_', ucwords(str_replace('_', ' ', $block)));
$blockClassSuffix = ucwords($block, '_');

/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ protected function getCurrentRewritesMocks($currentRewrites)
->disableOriginalConstructor()->getMock();
foreach ($urlRewrite as $key => $value) {
$url->expects($this->any())
->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
->method('get' . str_replace('_', '', ucwords($key, '_')))
->will($this->returnValue($value));
}
$rewrites[] = $url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected function getCurrentRewritesMocks($currentRewrites)
->disableOriginalConstructor()->getMock();
foreach ($urlRewrite as $key => $value) {
$url->expects($this->any())
->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
->method('get' . str_replace('_', '', ucwords($key, '_')))
->will($this->returnValue($value));
}
$rewrites[] = $url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ protected function currentUrlRewritesRegeneratorGetCurrentRewritesMocks($current
->disableOriginalConstructor()->getMock();
foreach ($urlRewrite as $key => $value) {
$url->expects($this->any())
->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
->method('get' . str_replace('_', '', ucwords($key, '_')))
->will($this->returnValue($value));
}
$rewrites[] = $url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private function getTaxRateMock(array $taxRateData)
foreach ($taxRateData as $key => $value) {
// convert key from snake case to upper case
$taxRateMock->expects($this->any())
->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
->method('get' . str_replace('_', '', ucwords($key, '_')))
->will($this->returnValue($value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function getMockObject($className, array $objectState)
$getterValueMap = [];
$methods = ['__wakeup'];
foreach ($objectState as $key => $value) {
$getterName = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
$getterName = 'get' . str_replace('_', '', ucwords($key, '_'));
$getterValueMap[$getterName] = $value;
$methods[] = $getterName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function createProducts(FixtureFactory $fixtureFactory, $productsData)
$searchValue = isset($productData[2]) ? $productData[2] : $productData[1];
if ($this->data === null) {
if ($product->hasData($searchValue)) {
$getProperty = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $searchValue)));
$getProperty = 'get' . str_replace('_', '', ucwords($searchValue, '_'));
$this->data = $product->$getProperty();
} else {
$this->data = $searchValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected function prepareWidgetInstance(array $data)
$widgetInstances = [];
foreach ($data['widget_instance'] as $key => $widgetInstance) {
$pageGroup = $widgetInstance['page_group'];
$method = 'prepare' . str_replace(' ', '', ucwords(str_replace('_', ' ', $pageGroup))) . 'Group';
$method = 'prepare' . str_replace('_', '', ucwords($pageGroup, '_')) . 'Group';
if (!method_exists(__CLASS__, $method)) {
throw new \Exception('Method for prepare page group "' . $method . '" is not exist.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function convertKeysToCamelCase(array $dataArray)
if (is_array($fieldValue) && !$this->_isSimpleSequentialArray($fieldValue)) {
$fieldValue = $this->convertKeysToCamelCase($fieldValue);
}
$fieldName = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName))));
$fieldName = lcfirst(str_replace('_', '', ucwords($fieldName, '_')));
$response[$fieldName] = $fieldValue;
}
return $response;
Expand Down Expand Up @@ -148,7 +148,7 @@ protected function _unpackAssociativeArray($data)
*/
public static function snakeCaseToUpperCamelCase($input)
{
return str_replace(' ', '', ucwords(str_replace('_', ' ', $input)));
return str_replace('_', '', ucwords($input, '_'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/Magento/Framework/Code/NameBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function buildClassName($parts)
$separator = '\\';
$string = join($separator, $parts);
$string = str_replace('_', $separator, $string);
$className = str_replace(' ', $separator, ucwords(str_replace($separator, ' ', $string)));
$className = ucwords($string, $separator);
return $className;
}
}
17 changes: 9 additions & 8 deletions lib/internal/Magento/Framework/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public function addData(array $arr)
*
* If $key is an array, it will overwrite all the data in the object.
*
* @param string|array $key
* @param mixed $value
* @param string|array $key
* @param mixed $value
* @return $this
*/
public function setData($key, $value = null)
Expand Down Expand Up @@ -111,7 +111,7 @@ public function unsetData($key = null)
* and retrieve corresponding member. If data is the string - it will be explode
* by new line character and converted to array.
*
* @param string $key
* @param string $key
* @param string|int $index
* @return mixed
*/
Expand Down Expand Up @@ -202,7 +202,7 @@ protected function _getData($key)
*/
public function setDataUsingMethod($key, $args = [])
{
$method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
$this->{$method}($args);
return $this;
}
Expand All @@ -216,12 +216,13 @@ public function setDataUsingMethod($key, $args = [])
*/
public function getDataUsingMethod($key, $args = null)
{
$method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
$method = 'get' . str_replace('_', '', ucwords($key, '_'));
return $this->{$method}($args);
}

/**
* If $key is empty, checks whether there's any data in the object
*
* Otherwise checks if the specified attribute is set.
*
* @param string $key
Expand Down Expand Up @@ -272,8 +273,8 @@ public function convertToArray(array $keys = [])
/**
* Convert object data into XML string
*
* @param array $keys array of keys that must be represented
* @param string $rootName root node name
* @param array $keys array of keys that must be represented
* @param string $rootName root node name
* @param bool $addOpenTag flag that allow to add initial xml node
* @param bool $addCdata flag that require wrap all values in CDATA
* @return string
Expand Down Expand Up @@ -436,7 +437,7 @@ protected function _underscore($name)
*
* Example: key1="value1" key2="value2" ...
*
* @param array $keys array of accepted keys
* @param array $keys array of accepted keys
* @param string $valueSeparator separator between key and value
* @param string $fieldSeparator separator between key/value pairs
* @param string $quote quoting sign
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/Magento/Framework/DataObject/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected function _setFieldsetFieldValue($target, $targetCode, $value)
*/
protected function getAttributeValueFromExtensibleDataObject($source, $code)
{
$method = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
$method = 'get' . str_replace('_', '', ucwords($code, '_'));

$methodExists = method_exists($source, $method);
if ($methodExists == true) {
Expand Down Expand Up @@ -273,7 +273,7 @@ protected function getAttributeValueFromExtensibleDataObject($source, $code)
*/
protected function setAttributeValueFromExtensibleDataObject($target, $code, $value)
{
$method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
$method = 'set' . str_replace('_', '', ucwords($code, '_'));

$methodExists = method_exists($target, $method);
if ($methodExists == true) {
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/Magento/Framework/Module/PackageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Magento\Framework\Component\ComponentRegistrar;

/**
* Provide information of dependencies and conflicts in composer.json files, mapping of package name to module name,
* and mapping of module name to package version
* Provide information of dependencies and conflicts in composer.json files.
*
* Mapping of package name to module name, and mapping of module name to package version.
*/
class PackageInfo
{
Expand Down Expand Up @@ -176,8 +177,7 @@ public function getNonExistingDependencies()
protected function convertPackageNameToModuleName($packageName)
{
$moduleName = str_replace('magento/module-', '', $packageName);
$moduleName = str_replace('-', ' ', $moduleName);
$moduleName = str_replace(' ', '', ucwords($moduleName));
$moduleName = str_replace('-', '', ucwords($moduleName, '-'));

return 'Magento_' . $moduleName;
}
Expand Down

0 comments on commit 371d67b

Please sign in to comment.