Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Forwardport] Code cleanup, add more visibility #15069

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 37 additions & 64 deletions app/code/Magento/Rule/Model/Condition/AbstractCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ abstract class AbstractCondition extends \Magento\Framework\DataObject implement
{
/**
* Defines which operators will be available for this condition
*
* @var string
*/
protected $_inputType = null;
Expand Down Expand Up @@ -84,17 +83,13 @@ public function __construct(Context $context, array $data = [])

$options = $this->getAttributeOptions();
if ($options) {
foreach (array_keys($options) as $attr) {
$this->setAttribute($attr);
break;
}
reset($options);
$this->setAttribute(key($options));
}
$options = $this->getOperatorOptions();
if ($options) {
foreach (array_keys($options) as $operator) {
$this->setOperator($operator);
break;
}
reset($options);
$this->setOperator(key($options));
}
}

Expand Down Expand Up @@ -160,14 +155,13 @@ public function getForm()
*/
public function asArray(array $arrAttributes = [])
{
$out = [
return [
'type' => $this->getType(),
'attribute' => $this->getAttribute(),
'operator' => $this->getOperator(),
'value' => $this->getValue(),
'is_value_processed' => $this->getIsValueParsed(),
];
return $out;
}

/**
Expand Down Expand Up @@ -205,7 +199,7 @@ public function getMappedSqlField()
*/
public function asXml()
{
$xml = "<type>" .
return "<type>" .
$this->getType() .
"</type>" .
"<attribute>" .
Expand All @@ -217,7 +211,6 @@ public function asXml()
"<value>" .
$this->getValue() .
"</value>";
return $xml;
}

/**
Expand All @@ -244,8 +237,7 @@ public function loadXml($xml)
if (is_string($xml)) {
$xml = simplexml_load_string($xml);
}
$arr = (array)$xml;
$this->loadArray($arr);
$this->loadArray((array)$xml);
return $this;
}

Expand Down Expand Up @@ -304,10 +296,7 @@ public function loadOperatorOptions()
*/
public function getInputType()
{
if (null === $this->_inputType) {
return 'string';
}
return $this->_inputType;
return null === $this->_inputType ? 'string' : $this->_inputType;
}

/**
Expand Down Expand Up @@ -348,12 +337,11 @@ public function loadValueOptions()
*/
public function getValueSelectOptions()
{
$valueOption = $opt = [];
$opt = [];
if ($this->hasValueOption()) {
$valueOption = (array)$this->getValueOption();
}
foreach ($valueOption as $key => $value) {
$opt[] = ['value' => $key, 'label' => $value];
foreach ((array)$this->getValueOption() as $key => $value) {
$opt[] = ['value' => $key, 'label' => $value];
}
}
return $opt;
}
Expand Down Expand Up @@ -470,22 +458,20 @@ public function getNewChildName()
*/
public function asHtml()
{
$html = $this->getTypeElementHtml() .
return $this->getTypeElementHtml() .
$this->getAttributeElementHtml() .
$this->getOperatorElementHtml() .
$this->getValueElementHtml() .
$this->getRemoveLinkHtml() .
$this->getChooserContainerHtml();
return $html;
}

/**
* @return string
*/
public function asHtmlRecursive()
{
$html = $this->asHtml();
return $html;
return $this->asHtml();
}

/**
Expand Down Expand Up @@ -520,9 +506,10 @@ public function getTypeElementHtml()
public function getAttributeElement()
{
if (null === $this->getAttribute()) {
foreach (array_keys($this->getAttributeOption()) as $option) {
$this->setAttribute($option);
break;
$options = $this->getAttributeOption();
if ($options) {
reset($options);
$this->setAttribute(key($options));
}
}
return $this->getForm()->addField(
Expand Down Expand Up @@ -558,10 +545,8 @@ public function getOperatorElement()
{
$options = $this->getOperatorSelectOptions();
if ($this->getOperator() === null) {
foreach ($options as $option) {
$this->setOperator($option['value']);
break;
}
$option = reset($options);
$this->setOperator($option['value']);
}

$elementId = sprintf('%s__%s__operator', $this->getPrefix(), $this->getId());
Expand Down Expand Up @@ -654,8 +639,7 @@ public function getValueElementHtml()
public function getAddLinkHtml()
{
$src = $this->_assetRepo->getUrl('images/rule_component_add.gif');
$html = '<img src="' . $src . '" class="rule-param-add v-middle" alt="" title="' . __('Add') . '"/>';
return $html;
return '<img src="' . $src . '" class="rule-param-add v-middle" alt="" title="' . __('Add') . '"/>';
}

/**
Expand All @@ -676,11 +660,7 @@ public function getRemoveLinkHtml()
public function getChooserContainerHtml()
{
$url = $this->getValueElementChooserUrl();
$html = '';
if ($url) {
$html = '<div class="rule-chooser" url="' . $url . '"></div>';
}
return $html;
return $url ? '<div class="rule-chooser" url="' . $url . '"></div>' : '';
}

/**
Expand All @@ -690,8 +670,7 @@ public function getChooserContainerHtml()
*/
public function asString($format = '')
{
$str = $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
return $str;
return $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
}

/**
Expand All @@ -700,8 +679,7 @@ public function asString($format = '')
*/
public function asStringRecursive($level = 0)
{
$str = str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
return $str;
return str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
}

/**
Expand Down Expand Up @@ -740,12 +718,10 @@ public function validateAttribute($validatedValue)
case '==':
case '!=':
if (is_array($value)) {
if (is_array($validatedValue)) {
$result = array_intersect($value, $validatedValue);
$result = !empty($result);
} else {
if (!is_array($validatedValue)) {
return false;
}
$result = !empty(array_intersect($value, $validatedValue));
} else {
if (is_array($validatedValue)) {
$result = count($validatedValue) == 1 && array_shift($validatedValue) == $value;
Expand All @@ -759,18 +735,16 @@ public function validateAttribute($validatedValue)
case '>':
if (!is_scalar($validatedValue)) {
return false;
} else {
$result = $validatedValue <= $value;
}
$result = $validatedValue <= $value;
break;

case '>=':
case '<':
if (!is_scalar($validatedValue)) {
return false;
} else {
$result = $validatedValue >= $value;
}
$result = $validatedValue >= $value;
break;

case '{}':
Expand All @@ -783,12 +757,11 @@ public function validateAttribute($validatedValue)
}
}
} elseif (is_array($value)) {
if (is_array($validatedValue)) {
$result = array_intersect($value, $validatedValue);
$result = !empty($result);
} else {
if (!is_array($validatedValue)) {
return false;
}
$result = array_intersect($value, $validatedValue);
$result = !empty($result);
} else {
if (is_array($validatedValue)) {
$result = in_array($value, $validatedValue);
Expand Down Expand Up @@ -833,13 +806,13 @@ protected function _compareValues($validatedValue, $value, $strict = true)
{
if ($strict && is_numeric($validatedValue) && is_numeric($value)) {
return $validatedValue == $value;
} else {
$validatePattern = preg_quote($validatedValue, '~');
if ($strict) {
$validatePattern = '^' . $validatePattern . '$';
}
return (bool)preg_match('~' . $validatePattern . '~iu', $value);
}

$validatePattern = preg_quote($validatedValue, '~');
if ($strict) {
$validatePattern = '^' . $validatePattern . '$';
}
return (bool)preg_match('~' . $validatePattern . '~iu', $value);
}

/**
Expand Down