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

Fix matching for '@array@||@null@' #493

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions src/Factory/MatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,22 @@ private function buildMatchers(Parser $parser, Backtrace $backtrace) : Matcher\C

private function buildArrayMatcher(Matcher\ChainMatcher $scalarMatchers, Parser $parser, Backtrace $backtrace) : Matcher\ArrayMatcher
{
$orMatcher = new Matcher\OrMatcher($backtrace, $scalarMatchers);

return new Matcher\ArrayMatcher(
$arrayMatcher = new Matcher\ArrayMatcher(
new Matcher\ChainMatcher(
'array',
$backtrace,
[
$orMatcher,
new Matcher\OrMatcher($backtrace, $orMatchers = clone $scalarMatchers),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried it without a clone?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the clone works but I added it here for a specific reason. Since the factory describes the following matcher priorities:

// Matchers are registered in order of matching
// 1) all scalars
// 2) json/xml
// 3) array
// 4) or "||"
// 5) full text

I have decided to clone the scalair matchers in order to keep theses priorities on the main matcher.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and without clone that order is changed?

$scalarMatchers,
new Matcher\TextMatcher($backtrace, $parser),
]
),
$backtrace,
$parser
);
$orMatchers->registerMatcher($arrayMatcher);

return $arrayMatcher;
}

private function buildScalarMatchers(Parser $parser, Backtrace $backtrace) : Matcher\ChainMatcher
Expand Down
5 changes: 5 additions & 0 deletions src/Matcher/ChainMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function __construct(string $name, Backtrace $backtrace, array $matchers
$this->name = $name;
}

public function registerMatcher(ValueMatcher $matcher) : void
{
$this->matchers[] = $matcher;
}

public function match($value, $pattern) : bool
{
$this->backtrace->matcherEntrance($this->matcherName(), $value, $pattern);
Expand Down
8 changes: 8 additions & 0 deletions tests/Matcher/OrMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public static function positiveMatchData()
'test' => '@integer@||@null@',
],
],
[
[
'test' => [],
],
[
'test' => '@array@||@null@',
],
],
[
[
'first_level' => ['second_level', ['third_level']],
Expand Down
Loading