Skip to content

Commit

Permalink
Make Backtrace optional (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Jul 18, 2020
1 parent 0c8fff1 commit 60c1b8c
Show file tree
Hide file tree
Showing 55 changed files with 396 additions and 247 deletions.
70 changes: 28 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ class MatcherTest extends TestCase
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(1, 1);
$matcher->match('string', 'string');
Expand All @@ -148,10 +147,9 @@ $matcher->match('string', 'string');
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match('Norbert', '@string@');
$matcher->match("lorem ipsum dolor", "@[email protected]('lorem').contains('ipsum').endsWith('dolor')");
Expand All @@ -163,10 +161,9 @@ $matcher->match("lorem ipsum dolor", "@[email protected]('lorem').contains('ips
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match('2014-08-19', '@[email protected]()');
$matcher->match('2014-08-19', '@[email protected]().before("2016-08-19")');
Expand All @@ -179,10 +176,9 @@ $matcher->match('2014-08-19', '@[email protected]().before("today").after("+ 10
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(100, '@integer@');
$matcher->match(100, '@[email protected](200).greaterThan(10)');
Expand All @@ -194,10 +190,9 @@ $matcher->match(100, '@[email protected](200).greaterThan(10)');
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(100, '@number@');
$matcher->match('200', '@number@');
Expand All @@ -211,10 +206,9 @@ $matcher->match(0b10100111001, '@number@');
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(10.1, "@double@");
$matcher->match(10.1, "@[email protected](50.12).greaterThan(10)");
Expand All @@ -225,10 +219,9 @@ $matcher->match(10.1, "@[email protected](50.12).greaterThan(10)");
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(true, "@boolean@");
$matcher->match(false, "@boolean@");
Expand All @@ -239,10 +232,9 @@ $matcher->match(false, "@boolean@");
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match("@integer@", "@*@");
$matcher->match("foobar", "@*@");
Expand All @@ -257,10 +249,9 @@ $matcher->match(new \stdClass, "@wildcard@");
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01'");
$matcher->match("Norbert", "expr(value === 'Norbert')");
Expand All @@ -271,10 +262,9 @@ $matcher->match("Norbert", "expr(value === 'Norbert')");
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
```
Expand All @@ -284,10 +274,9 @@ $matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(
array(
Expand Down Expand Up @@ -343,10 +332,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(
'{
Expand Down Expand Up @@ -378,10 +366,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(
'{
Expand Down Expand Up @@ -442,10 +429,9 @@ $matcher->match(
```php
<?php

use Coduo\PHPMatcher\Factory\MatcherFactory;
use Coduo\PHPMatcher\PHPMatcher;

$factory = new MatcherFactory();
$matcher = $factory->createMatcher();
$matcher = new PHPMatcher();

$matcher->match(<<<XML
<?xml version="1.0"?>
Expand Down
33 changes: 33 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
# 4.0 -> 5.0

**Backtrace**

In order to improve performance of matcher `Backtrace` class was replaced `InMemoryBacktrace`
that implements `Backtrace` interface.

In order to use backtrace provide it directly to `MatcherFactory` or `PHPMatcher` class.

PHPUnit tests require `setBacktrace` method to be used before test:

```php
$this->setBacktrace($backtrace = new InMemoryBacktrace());
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(['foo' => 'bar']));
```

**Optional Matchers**

XML and Expression matchers are now optional, in order to use them add following
dependencies to your composer.json file:

XMLMatcher

```
"openlss/lib-array2xml": "^1.0"
```

ExpressionMatcher

```
symfony/expression-language
```

# 3.x -> 4.0

Below you can find list of changes between `3.x` and `4.0` versions of PHPMatcher.
Expand Down
126 changes: 11 additions & 115 deletions src/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,129 +4,25 @@

namespace Coduo\PHPMatcher;

use Coduo\PHPMatcher\Value\SingleLineString;
use Coduo\ToString\StringConverter;
use function sprintf;
use function implode;
use function count;

final class Backtrace
interface Backtrace
{
/**
* @var mixed[]
*/
private $trace;

public function __construct()
{
$this->trace = [];
}

public function matcherCanMatch(string $name, $value, bool $result) : void
{
$this->trace[] = sprintf(
'#%d Matcher %s %s match pattern "%s"',
$this->entriesCount(),
$name,
$result ? 'can' : 'can\'t',
new SingleLineString((string) new StringConverter($value))
);
}

public function matcherEntrance(string $name, $value, $pattern) : void
{
$this->trace[] = sprintf(
'#%d Matcher %s matching value "%s" with "%s" pattern',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value)),
new SingleLineString((string) new StringConverter($pattern))
);
}

public function matcherSucceed(string $name, $value, $pattern) : void
{
$this->trace[] = sprintf(
'#%d Matcher %s successfully matched value "%s" with "%s" pattern',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value)),
new SingleLineString((string) new StringConverter($pattern))
);
}

public function matcherFailed(string $name, $value, $pattern, string $error) : void
{
$this->trace[] = sprintf(
'#%d Matcher %s failed to match value "%s" with "%s" pattern',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value)),
new SingleLineString((string) new StringConverter($pattern))
);
public function matcherCanMatch(string $name, $value, bool $result) : void;

$this->trace[] = sprintf(
'#%d Matcher %s error: %s',
$this->entriesCount(),
$name,
new SingleLineString($error)
);
}
public function matcherEntrance(string $name, $value, $pattern) : void;

public function expanderEntrance(string $name, $value) : void
{
$this->trace[] = sprintf(
'#%d Expander %s matching value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);
}
public function matcherSucceed(string $name, $value, $pattern) : void;

public function expanderSucceed(string $name, $value) : void
{
$this->trace[] = sprintf(
'#%d Expander %s successfully matched value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);
}
public function matcherFailed(string $name, $value, $pattern, string $error) : void;

public function expanderFailed(string $name, $value, string $error) : void
{
$this->trace[] = sprintf(
'#%d Expander %s failed to match value "%s"',
$this->entriesCount(),
$name,
new SingleLineString((string) new StringConverter($value))
);
public function expanderEntrance(string $name, $value) : void;

$this->trace[] = sprintf(
'#%d Expander %s error: %s',
$this->entriesCount(),
$name,
new SingleLineString($error)
);
}
public function expanderSucceed(string $name, $value) : void;

public function isEmpty() : bool
{
return count($this->trace) === 0;
}
public function expanderFailed(string $name, $value, string $error) : void;

public function __toString() : string
{
return implode("\n", $this->trace);
}
public function isEmpty() : bool;

public function raw() : array
{
return $this->trace;
}
public function __toString() : string;

private function entriesCount(): int
{
return count($this->trace) + 1;
}
public function raw() : array;
}
Loading

0 comments on commit 60c1b8c

Please sign in to comment.