Skip to content

Commit 60c1b8c

Browse files
authored
Make Backtrace optional (#205)
1 parent 0c8fff1 commit 60c1b8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+396
-247
lines changed

README.md

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,9 @@ class MatcherTest extends TestCase
134134
```php
135135
<?php
136136

137-
use Coduo\PHPMatcher\Factory\MatcherFactory;
137+
use Coduo\PHPMatcher\PHPMatcher;
138138

139-
$factory = new MatcherFactory();
140-
$matcher = $factory->createMatcher();
139+
$matcher = new PHPMatcher();
141140

142141
$matcher->match(1, 1);
143142
$matcher->match('string', 'string');
@@ -148,10 +147,9 @@ $matcher->match('string', 'string');
148147
```php
149148
<?php
150149

151-
use Coduo\PHPMatcher\Factory\MatcherFactory;
150+
use Coduo\PHPMatcher\PHPMatcher;
152151

153-
$factory = new MatcherFactory();
154-
$matcher = $factory->createMatcher();
152+
$matcher = new PHPMatcher();
155153

156154
$matcher->match('Norbert', '@string@');
157155
$matcher->match("lorem ipsum dolor", "@string@.startsWith('lorem').contains('ipsum').endsWith('dolor')");
@@ -163,10 +161,9 @@ $matcher->match("lorem ipsum dolor", "@string@.startsWith('lorem').contains('ips
163161
```php
164162
<?php
165163

166-
use Coduo\PHPMatcher\Factory\MatcherFactory;
164+
use Coduo\PHPMatcher\PHPMatcher;
167165

168-
$factory = new MatcherFactory();
169-
$matcher = $factory->createMatcher();
166+
$matcher = new PHPMatcher();
170167

171168
$matcher->match('2014-08-19', '@string@.isDateTime()');
172169
$matcher->match('2014-08-19', '@string@.isDateTime().before("2016-08-19")');
@@ -179,10 +176,9 @@ $matcher->match('2014-08-19', '@string@.isDateTime().before("today").after("+ 10
179176
```php
180177
<?php
181178

182-
use Coduo\PHPMatcher\Factory\MatcherFactory;
179+
use Coduo\PHPMatcher\PHPMatcher;
183180

184-
$factory = new MatcherFactory();
185-
$matcher = $factory->createMatcher();
181+
$matcher = new PHPMatcher();
186182

187183
$matcher->match(100, '@integer@');
188184
$matcher->match(100, '@integer@.lowerThan(200).greaterThan(10)');
@@ -194,10 +190,9 @@ $matcher->match(100, '@integer@.lowerThan(200).greaterThan(10)');
194190
```php
195191
<?php
196192

197-
use Coduo\PHPMatcher\Factory\MatcherFactory;
193+
use Coduo\PHPMatcher\PHPMatcher;
198194

199-
$factory = new MatcherFactory();
200-
$matcher = $factory->createMatcher();
195+
$matcher = new PHPMatcher();
201196

202197
$matcher->match(100, '@number@');
203198
$matcher->match('200', '@number@');
@@ -211,10 +206,9 @@ $matcher->match(0b10100111001, '@number@');
211206
```php
212207
<?php
213208

214-
use Coduo\PHPMatcher\Factory\MatcherFactory;
209+
use Coduo\PHPMatcher\PHPMatcher;
215210

216-
$factory = new MatcherFactory();
217-
$matcher = $factory->createMatcher();
211+
$matcher = new PHPMatcher();
218212

219213
$matcher->match(10.1, "@double@");
220214
$matcher->match(10.1, "@double@.lowerThan(50.12).greaterThan(10)");
@@ -225,10 +219,9 @@ $matcher->match(10.1, "@double@.lowerThan(50.12).greaterThan(10)");
225219
```php
226220
<?php
227221

228-
use Coduo\PHPMatcher\Factory\MatcherFactory;
222+
use Coduo\PHPMatcher\PHPMatcher;
229223

230-
$factory = new MatcherFactory();
231-
$matcher = $factory->createMatcher();
224+
$matcher = new PHPMatcher();
232225

233226
$matcher->match(true, "@boolean@");
234227
$matcher->match(false, "@boolean@");
@@ -239,10 +232,9 @@ $matcher->match(false, "@boolean@");
239232
```php
240233
<?php
241234

242-
use Coduo\PHPMatcher\Factory\MatcherFactory;
235+
use Coduo\PHPMatcher\PHPMatcher;
243236

244-
$factory = new MatcherFactory();
245-
$matcher = $factory->createMatcher();
237+
$matcher = new PHPMatcher();
246238

247239
$matcher->match("@integer@", "@*@");
248240
$matcher->match("foobar", "@*@");
@@ -257,10 +249,9 @@ $matcher->match(new \stdClass, "@wildcard@");
257249
```php
258250
<?php
259251

260-
use Coduo\PHPMatcher\Factory\MatcherFactory;
252+
use Coduo\PHPMatcher\PHPMatcher;
261253

262-
$factory = new MatcherFactory();
263-
$matcher = $factory->createMatcher();
254+
$matcher = new PHPMatcher();
264255

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

274-
use Coduo\PHPMatcher\Factory\MatcherFactory;
265+
use Coduo\PHPMatcher\PHPMatcher;
275266

276-
$factory = new MatcherFactory();
277-
$matcher = $factory->createMatcher();
267+
$matcher = new PHPMatcher();
278268

279269
$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
280270
```
@@ -284,10 +274,9 @@ $matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
284274
```php
285275
<?php
286276

287-
use Coduo\PHPMatcher\Factory\MatcherFactory;
277+
use Coduo\PHPMatcher\PHPMatcher;
288278

289-
$factory = new MatcherFactory();
290-
$matcher = $factory->createMatcher();
279+
$matcher = new PHPMatcher();
291280

292281
$matcher->match(
293282
array(
@@ -343,10 +332,9 @@ $matcher->match(
343332
```php
344333
<?php
345334

346-
use Coduo\PHPMatcher\Factory\MatcherFactory;
335+
use Coduo\PHPMatcher\PHPMatcher;
347336

348-
$factory = new MatcherFactory();
349-
$matcher = $factory->createMatcher();
337+
$matcher = new PHPMatcher();
350338

351339
$matcher->match(
352340
'{
@@ -378,10 +366,9 @@ $matcher->match(
378366
```php
379367
<?php
380368

381-
use Coduo\PHPMatcher\Factory\MatcherFactory;
369+
use Coduo\PHPMatcher\PHPMatcher;
382370

383-
$factory = new MatcherFactory();
384-
$matcher = $factory->createMatcher();
371+
$matcher = new PHPMatcher();
385372

386373
$matcher->match(
387374
'{
@@ -442,10 +429,9 @@ $matcher->match(
442429
```php
443430
<?php
444431

445-
use Coduo\PHPMatcher\Factory\MatcherFactory;
432+
use Coduo\PHPMatcher\PHPMatcher;
446433

447-
$factory = new MatcherFactory();
448-
$matcher = $factory->createMatcher();
434+
$matcher = new PHPMatcher();
449435

450436
$matcher->match(<<<XML
451437
<?xml version="1.0"?>

UPGRADE.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
# 4.0 -> 5.0
2+
3+
**Backtrace**
4+
5+
In order to improve performance of matcher `Backtrace` class was replaced `InMemoryBacktrace`
6+
that implements `Backtrace` interface.
7+
8+
In order to use backtrace provide it directly to `MatcherFactory` or `PHPMatcher` class.
9+
10+
PHPUnit tests require `setBacktrace` method to be used before test:
11+
12+
```php
13+
$this->setBacktrace($backtrace = new InMemoryBacktrace());
14+
$this->assertMatchesPattern('{"foo": "@integer@"}', json_encode(['foo' => 'bar']));
15+
```
16+
17+
**Optional Matchers**
18+
19+
XML and Expression matchers are now optional, in order to use them add following
20+
dependencies to your composer.json file:
21+
22+
XMLMatcher
23+
24+
```
25+
"openlss/lib-array2xml": "^1.0"
26+
```
27+
28+
ExpressionMatcher
29+
30+
```
31+
symfony/expression-language
32+
```
33+
134
# 3.x -> 4.0
235

336
Below you can find list of changes between `3.x` and `4.0` versions of PHPMatcher.

src/Backtrace.php

Lines changed: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,129 +4,25 @@
44

55
namespace Coduo\PHPMatcher;
66

7-
use Coduo\PHPMatcher\Value\SingleLineString;
8-
use Coduo\ToString\StringConverter;
9-
use function sprintf;
10-
use function implode;
11-
use function count;
12-
13-
final class Backtrace
7+
interface Backtrace
148
{
15-
/**
16-
* @var mixed[]
17-
*/
18-
private $trace;
19-
20-
public function __construct()
21-
{
22-
$this->trace = [];
23-
}
24-
25-
public function matcherCanMatch(string $name, $value, bool $result) : void
26-
{
27-
$this->trace[] = sprintf(
28-
'#%d Matcher %s %s match pattern "%s"',
29-
$this->entriesCount(),
30-
$name,
31-
$result ? 'can' : 'can\'t',
32-
new SingleLineString((string) new StringConverter($value))
33-
);
34-
}
35-
36-
public function matcherEntrance(string $name, $value, $pattern) : void
37-
{
38-
$this->trace[] = sprintf(
39-
'#%d Matcher %s matching value "%s" with "%s" pattern',
40-
$this->entriesCount(),
41-
$name,
42-
new SingleLineString((string) new StringConverter($value)),
43-
new SingleLineString((string) new StringConverter($pattern))
44-
);
45-
}
46-
47-
public function matcherSucceed(string $name, $value, $pattern) : void
48-
{
49-
$this->trace[] = sprintf(
50-
'#%d Matcher %s successfully matched value "%s" with "%s" pattern',
51-
$this->entriesCount(),
52-
$name,
53-
new SingleLineString((string) new StringConverter($value)),
54-
new SingleLineString((string) new StringConverter($pattern))
55-
);
56-
}
57-
58-
public function matcherFailed(string $name, $value, $pattern, string $error) : void
59-
{
60-
$this->trace[] = sprintf(
61-
'#%d Matcher %s failed to match value "%s" with "%s" pattern',
62-
$this->entriesCount(),
63-
$name,
64-
new SingleLineString((string) new StringConverter($value)),
65-
new SingleLineString((string) new StringConverter($pattern))
66-
);
9+
public function matcherCanMatch(string $name, $value, bool $result) : void;
6710

68-
$this->trace[] = sprintf(
69-
'#%d Matcher %s error: %s',
70-
$this->entriesCount(),
71-
$name,
72-
new SingleLineString($error)
73-
);
74-
}
11+
public function matcherEntrance(string $name, $value, $pattern) : void;
7512

76-
public function expanderEntrance(string $name, $value) : void
77-
{
78-
$this->trace[] = sprintf(
79-
'#%d Expander %s matching value "%s"',
80-
$this->entriesCount(),
81-
$name,
82-
new SingleLineString((string) new StringConverter($value))
83-
);
84-
}
13+
public function matcherSucceed(string $name, $value, $pattern) : void;
8514

86-
public function expanderSucceed(string $name, $value) : void
87-
{
88-
$this->trace[] = sprintf(
89-
'#%d Expander %s successfully matched value "%s"',
90-
$this->entriesCount(),
91-
$name,
92-
new SingleLineString((string) new StringConverter($value))
93-
);
94-
}
15+
public function matcherFailed(string $name, $value, $pattern, string $error) : void;
9516

96-
public function expanderFailed(string $name, $value, string $error) : void
97-
{
98-
$this->trace[] = sprintf(
99-
'#%d Expander %s failed to match value "%s"',
100-
$this->entriesCount(),
101-
$name,
102-
new SingleLineString((string) new StringConverter($value))
103-
);
17+
public function expanderEntrance(string $name, $value) : void;
10418

105-
$this->trace[] = sprintf(
106-
'#%d Expander %s error: %s',
107-
$this->entriesCount(),
108-
$name,
109-
new SingleLineString($error)
110-
);
111-
}
19+
public function expanderSucceed(string $name, $value) : void;
11220

113-
public function isEmpty() : bool
114-
{
115-
return count($this->trace) === 0;
116-
}
21+
public function expanderFailed(string $name, $value, string $error) : void;
11722

118-
public function __toString() : string
119-
{
120-
return implode("\n", $this->trace);
121-
}
23+
public function isEmpty() : bool;
12224

123-
public function raw() : array
124-
{
125-
return $this->trace;
126-
}
25+
public function __toString() : string;
12726

128-
private function entriesCount(): int
129-
{
130-
return count($this->trace) + 1;
131-
}
27+
public function raw() : array;
13228
}

0 commit comments

Comments
 (0)