@@ -44,9 +44,10 @@ public function __construct(OutputInterface $output, MetaCharacters $meta, Escap
4444 * Serialize given strings into a regular expression
4545 *
4646 * @param array[] $strings
47+ * @param bool $groupAlternations Whether alternations should be parenthesized into a group
4748 * @return string
4849 */
49- public function serializeStrings (array $ strings ): string
50+ public function serializeStrings (array $ strings, bool $ groupAlternations = true ): string
5051 {
5152 $ info = $ this ->analyzeStrings ($ strings );
5253 $ alternations = array_map ([$ this , 'serializeString ' ], $ info ['strings ' ]);
@@ -57,7 +58,7 @@ public function serializeStrings(array $strings): string
5758 }
5859
5960 $ expr = implode ('| ' , $ alternations );
60- if ($ this ->needsParentheses ($ info ))
61+ if ($ this ->needsParentheses ($ info, $ groupAlternations ))
6162 {
6263 $ expr = '(?: ' . $ expr . ') ' ;
6364 }
@@ -189,11 +190,13 @@ protected function isSingleQuantifiableString(array $strings): bool
189190 * Test whether an expression needs parentheses based on the strings info
190191 *
191192 * @param array $info
193+ * @param bool $groupAlternations Whether alternations should be parenthesized into a group
192194 * @return bool
193195 */
194- protected function needsParentheses (array $ info ): bool
196+ protected function needsParentheses (array $ info, bool $ groupAlternations ): bool
195197 {
196- return ($ info ['alternationsCount ' ] > 1 || ($ info ['quantifier ' ] && !$ this ->isQuantifiable ($ info )));
198+ return (($ groupAlternations && $ info ['alternationsCount ' ] > 1 )
199+ || ($ info ['quantifier ' ] && !$ this ->isQuantifiable ($ info )));
197200 }
198201
199202 /**
0 commit comments