Skip to content

Commit c8a8ba9

Browse files
committed
Improve readability of mustache patterns
1 parent fd19fb5 commit c8a8ba9

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/Magento/FunctionalTestingFramework/Test/Objects/ActionObject.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class ActionObject
7676
const ACTION_ATTRIBUTE_USERINPUT = 'userInput';
7777
const ACTION_TYPE_COMMENT = 'comment';
7878
const INVISIBLE_STEP_ACTIONS = ['retrieveEntityField', 'getSecret'];
79+
const REGEX_SINGLE_GROUP = '[\w]+';
80+
const REGEX_WITH_INDEX = '[\w]+\.[\w\[\]]+';
81+
const REGEX_WITH_PARAM = '[\w]+\.[\w]+\((?(?!}}).)+\)';
7982

8083
/**
8184
* The unique identifier for the action
@@ -433,8 +436,13 @@ private function resolveUrlReference()
433436
*/
434437
private function getMissingReferences($replacement): array
435438
{
436-
$mustachePattern = '/({{[\w]+}})|({{[\w]+\.[\w\[\]]+}})|({{[\w]+\.[\w]+\((?(?!}}).)+\)}})/';
437-
preg_match_all($mustachePattern, $replacement, $matches);
439+
$matchPatterns = [
440+
self::REGEX_SINGLE_GROUP,
441+
self::REGEX_WITH_INDEX,
442+
self::REGEX_WITH_PARAM
443+
];
444+
445+
preg_match_all($this->getMustachePattern($matchPatterns), $replacement, $matches);
438446

439447
return array_filter($matches[1], function ($match) {
440448
return !empty($match) && false === strpos($match, '_ENV.');
@@ -534,10 +542,12 @@ private function stripAndReturnParameters($reference)
534542
*/
535543
private function findAndReplaceReferences($objectHandler, $inputString)
536544
{
537-
//look for parameter area, if so use different regex
538-
$regex = ActionObject::ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN;
545+
$matchPatterns = [
546+
self::REGEX_WITH_INDEX,
547+
self::REGEX_WITH_PARAM
548+
];
539549

540-
preg_match_all($regex, $inputString, $matches);
550+
preg_match_all($this->getMustachePattern($matchPatterns), $inputString, $matches);
541551

542552
$outputString = $inputString;
543553

@@ -720,7 +730,11 @@ private function resolveParameterization($isParameterized, $replacement, $match,
720730
*/
721731
private function matchParameterReferences($reference, $parameters)
722732
{
723-
preg_match_all('/{{[\w.]+}}/', $reference, $varMatches);
733+
$matchPatterns = [
734+
self::REGEX_SINGLE_GROUP
735+
];
736+
737+
preg_match_all($this->getMustachePattern($matchPatterns), $reference, $varMatches);
724738
$varMatches[0] = array_unique($varMatches[0]);
725739
$this->checkParameterCount($varMatches[0], $parameters, $reference);
726740

@@ -790,4 +804,15 @@ private function checkParameterCount($matches, $parameters, $reference)
790804
);
791805
}
792806
}
807+
808+
/**
809+
* Returns Mustache regex pattern
810+
*
811+
* @param array|null $patterns
812+
* @return string
813+
*/
814+
private function getMustachePattern(array $patterns = []): string
815+
{
816+
return '/({{' .implode('}})|({{', $patterns).'}})/';
817+
}
793818
}

0 commit comments

Comments
 (0)