Skip to content

Commit 6771f36

Browse files
Code cleanup, add more visibility
1 parent a53347e commit 6771f36

File tree

1 file changed

+34
-64
lines changed

1 file changed

+34
-64
lines changed

app/code/Magento/Rule/Model/Condition/AbstractCondition.php

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ abstract class AbstractCondition extends \Magento\Framework\DataObject implement
2424
{
2525
/**
2626
* Defines which operators will be available for this condition
27-
*
2827
* @var string
2928
*/
3029
protected $_inputType = null;
@@ -84,17 +83,11 @@ public function __construct(Context $context, array $data = [])
8483

8584
$options = $this->getAttributeOptions();
8685
if ($options) {
87-
foreach (array_keys($options) as $attr) {
88-
$this->setAttribute($attr);
89-
break;
90-
}
86+
$this->setAttribute(key($options));
9187
}
9288
$options = $this->getOperatorOptions();
9389
if ($options) {
94-
foreach (array_keys($options) as $operator) {
95-
$this->setOperator($operator);
96-
break;
97-
}
90+
$this->setOperator(key($options));
9891
}
9992
}
10093

@@ -160,14 +153,13 @@ public function getForm()
160153
*/
161154
public function asArray(array $arrAttributes = [])
162155
{
163-
$out = [
156+
return [
164157
'type' => $this->getType(),
165158
'attribute' => $this->getAttribute(),
166159
'operator' => $this->getOperator(),
167160
'value' => $this->getValue(),
168161
'is_value_processed' => $this->getIsValueParsed(),
169162
];
170-
return $out;
171163
}
172164

173165
/**
@@ -205,7 +197,7 @@ public function getMappedSqlField()
205197
*/
206198
public function asXml()
207199
{
208-
$xml = "<type>" .
200+
return "<type>" .
209201
$this->getType() .
210202
"</type>" .
211203
"<attribute>" .
@@ -217,7 +209,6 @@ public function asXml()
217209
"<value>" .
218210
$this->getValue() .
219211
"</value>";
220-
return $xml;
221212
}
222213

223214
/**
@@ -244,8 +235,7 @@ public function loadXml($xml)
244235
if (is_string($xml)) {
245236
$xml = simplexml_load_string($xml);
246237
}
247-
$arr = (array)$xml;
248-
$this->loadArray($arr);
238+
$this->loadArray((array)$xml);
249239
return $this;
250240
}
251241

@@ -304,10 +294,7 @@ public function loadOperatorOptions()
304294
*/
305295
public function getInputType()
306296
{
307-
if (null === $this->_inputType) {
308-
return 'string';
309-
}
310-
return $this->_inputType;
297+
return null === $this->_inputType ? 'string' : $this->_inputType;
311298
}
312299

313300
/**
@@ -348,12 +335,11 @@ public function loadValueOptions()
348335
*/
349336
public function getValueSelectOptions()
350337
{
351-
$valueOption = $opt = [];
338+
$opt = [];
352339
if ($this->hasValueOption()) {
353-
$valueOption = (array)$this->getValueOption();
354-
}
355-
foreach ($valueOption as $key => $value) {
356-
$opt[] = ['value' => $key, 'label' => $value];
340+
foreach ((array)$this->getValueOption() as $key => $value) {
341+
$opt[] = ['value' => $key, 'label' => $value];
342+
}
357343
}
358344
return $opt;
359345
}
@@ -470,22 +456,20 @@ public function getNewChildName()
470456
*/
471457
public function asHtml()
472458
{
473-
$html = $this->getTypeElementHtml() .
459+
return $this->getTypeElementHtml() .
474460
$this->getAttributeElementHtml() .
475461
$this->getOperatorElementHtml() .
476462
$this->getValueElementHtml() .
477463
$this->getRemoveLinkHtml() .
478464
$this->getChooserContainerHtml();
479-
return $html;
480465
}
481466

482467
/**
483468
* @return string
484469
*/
485470
public function asHtmlRecursive()
486471
{
487-
$html = $this->asHtml();
488-
return $html;
472+
return $this->asHtml();
489473
}
490474

491475
/**
@@ -520,9 +504,9 @@ public function getTypeElementHtml()
520504
public function getAttributeElement()
521505
{
522506
if (null === $this->getAttribute()) {
523-
foreach (array_keys($this->getAttributeOption()) as $option) {
524-
$this->setAttribute($option);
525-
break;
507+
$options = $this->getAttributeOptions();
508+
if ($options) {
509+
$this->setAttribute(key($options));
526510
}
527511
}
528512
return $this->getForm()->addField(
@@ -558,10 +542,8 @@ public function getOperatorElement()
558542
{
559543
$options = $this->getOperatorSelectOptions();
560544
if ($this->getOperator() === null) {
561-
foreach ($options as $option) {
562-
$this->setOperator($option['value']);
563-
break;
564-
}
545+
$option = current($options);
546+
$this->setOperator($option['value']);
565547
}
566548

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

661642
/**
@@ -676,11 +657,7 @@ public function getRemoveLinkHtml()
676657
public function getChooserContainerHtml()
677658
{
678659
$url = $this->getValueElementChooserUrl();
679-
$html = '';
680-
if ($url) {
681-
$html = '<div class="rule-chooser" url="' . $url . '"></div>';
682-
}
683-
return $html;
660+
return $url ? '<div class="rule-chooser" url="' . $url . '"></div>' : '';
684661
}
685662

686663
/**
@@ -690,8 +667,7 @@ public function getChooserContainerHtml()
690667
*/
691668
public function asString($format = '')
692669
{
693-
$str = $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
694-
return $str;
670+
return $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
695671
}
696672

697673
/**
@@ -700,8 +676,7 @@ public function asString($format = '')
700676
*/
701677
public function asStringRecursive($level = 0)
702678
{
703-
$str = str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
704-
return $str;
679+
return str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
705680
}
706681

707682
/**
@@ -740,12 +715,10 @@ public function validateAttribute($validatedValue)
740715
case '==':
741716
case '!=':
742717
if (is_array($value)) {
743-
if (is_array($validatedValue)) {
744-
$result = array_intersect($value, $validatedValue);
745-
$result = !empty($result);
746-
} else {
718+
if (!is_array($validatedValue)) {
747719
return false;
748720
}
721+
$result = !empty(array_intersect($value, $validatedValue));
749722
} else {
750723
if (is_array($validatedValue)) {
751724
$result = count($validatedValue) == 1 && array_shift($validatedValue) == $value;
@@ -759,18 +732,16 @@ public function validateAttribute($validatedValue)
759732
case '>':
760733
if (!is_scalar($validatedValue)) {
761734
return false;
762-
} else {
763-
$result = $validatedValue <= $value;
764735
}
736+
$result = $validatedValue <= $value;
765737
break;
766738

767739
case '>=':
768740
case '<':
769741
if (!is_scalar($validatedValue)) {
770742
return false;
771-
} else {
772-
$result = $validatedValue >= $value;
773743
}
744+
$result = $validatedValue >= $value;
774745
break;
775746

776747
case '{}':
@@ -783,12 +754,11 @@ public function validateAttribute($validatedValue)
783754
}
784755
}
785756
} elseif (is_array($value)) {
786-
if (is_array($validatedValue)) {
787-
$result = array_intersect($value, $validatedValue);
788-
$result = !empty($result);
789-
} else {
757+
if (!is_array($validatedValue)) {
790758
return false;
791759
}
760+
$result = array_intersect($value, $validatedValue);
761+
$result = !empty($result);
792762
} else {
793763
if (is_array($validatedValue)) {
794764
$result = in_array($value, $validatedValue);
@@ -833,13 +803,13 @@ protected function _compareValues($validatedValue, $value, $strict = true)
833803
{
834804
if ($strict && is_numeric($validatedValue) && is_numeric($value)) {
835805
return $validatedValue == $value;
836-
} else {
837-
$validatePattern = preg_quote($validatedValue, '~');
838-
if ($strict) {
839-
$validatePattern = '^' . $validatePattern . '$';
840-
}
841-
return (bool)preg_match('~' . $validatePattern . '~iu', $value);
842806
}
807+
808+
$validatePattern = preg_quote($validatedValue, '~');
809+
if ($strict) {
810+
$validatePattern = '^' . $validatePattern . '$';
811+
}
812+
return (bool)preg_match('~' . $validatePattern . '~iu', $value);
843813
}
844814

845815
/**

0 commit comments

Comments
 (0)