Skip to content

Commit 4006e85

Browse files
committed
correct preg_match usage
1 parent 8f8ff92 commit 4006e85

File tree

5 files changed

+36
-41
lines changed

5 files changed

+36
-41
lines changed

README.md

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ Documentation
129129
### Domain objects
130130

131131
~~~php
132-
<?php
133-
134132
use Pdp\Domain;
135133
use Pdp\PublicSuffix;
136134

@@ -152,6 +150,8 @@ iterator_to_array($domain, false); // ['com', 'example', 'bébé', 'www']
152150
$domain->labels(); // ['com', 'example', 'bébé', 'www'] since v5.5
153151
$domain->toAscii()->getContent(); // www.xn--bb-bjab.example.com
154152
echo (new Domain('www.xn--bb-bjab.example.com'))->toAscii() // www.bébé.example.com
153+
$domain->getAsciiIDNAOption(); // IDNA_DEFAULT
154+
$domain->getUnicodeIDNAOption(); // IDNA_DEFAULT
155155
~~~
156156

157157
The `Pdp\Domain` and `Pdp\PublicSuffix` objects are an immutable value object representing a valid domain name. These objects let's you access the domain properties.
@@ -175,8 +175,6 @@ public function Domain::withUnicodeIDNAOption(int $option): Domain
175175
~~~
176176

177177
~~~php
178-
<?php
179-
180178
use Pdp\Domain;
181179

182180
$domain = new Domain('www.bébé.be');
@@ -202,8 +200,6 @@ Because the `Pdp\Domain` object is immutable:
202200
The `Pdp\Domain` object can tell whether a public suffix can be attached to it using the `Pdp\Domain::isResolvable` method.
203201

204202
~~~php
205-
<?php
206-
207203
use Pdp\Domain;
208204

209205
$domain = new Domain('www.ExAmple.com');
@@ -258,21 +254,25 @@ namespace Pdp;
258254

259255
final class Rules
260256
{
257+
public function __construct(
258+
array $rules,
259+
int $asciiIDNAOption = IDNA_DEFAULT,
260+
int $unicodeIDNAOption = IDNA_DEFAULT
261+
): void
262+
261263
public static function createFromPath(
262-
string $path, $context = null,
264+
string $path,
265+
resource $context = null,
263266
int $asciiIDNAOption = IDNA_DEFAULT,
264267
int $unicodeIDNAOption = IDNA_DEFAULT
265268
): Rules
269+
266270
public static function createFromString(
267271
string $content,
268272
int $asciiIDNAOption = IDNA_DEFAULT,
269273
int $unicodeIDNAOption = IDNA_DEFAULT
270274
): Rules
271-
public function __construct(
272-
array $rules,
273-
int $asciiIDNAOption = IDNA_DEFAULT,
274-
int $unicodeIDNAOption = IDNA_DEFAULT
275-
): void
275+
276276
public function resolve($domain, string $section = ''): Domain
277277
public function getPublicSuffix($domain, string $section = ''): PublicSuffix
278278
public function getAsciiIDNAOption(): int
@@ -356,9 +356,17 @@ namespace Pdp;
356356

357357
final class TopLevelDomains implements Countable, IteratorAggregate
358358
{
359+
public function __construct(
360+
array $records,
361+
string $version,
362+
DateTimeInterface $modifiedDate,
363+
int $asciiIDNAOption = IDNA_DEFAULT,
364+
int $unicodeIDNAOption = IDNA_DEFAULT
365+
): void
366+
359367
public static function createFromPath(
360368
string $path,
361-
$context = null,
369+
resource $context = null,
362370
int $asciiIDNAOption = IDNA_DEFAULT,
363371
int $unicodeIDNAOption = IDNA_DEFAULT
364372
): TopLevelDomains
@@ -369,14 +377,6 @@ final class TopLevelDomains implements Countable, IteratorAggregate
369377
int $unicodeIDNAOption = IDNA_DEFAULT
370378
): TopLevelDomains
371379

372-
public function __construct(
373-
array $records,
374-
string $version,
375-
DateTimeInterface $modifiedDate,
376-
int $asciiIDNAOption = IDNA_DEFAULT,
377-
int $unicodeIDNAOption = IDNA_DEFAULT
378-
)
379-
380380
public function resolve($domain): Domain
381381
public function contains($domain): bool
382382
public function isEmpty(): bool
@@ -392,12 +392,12 @@ The `Pdp\TopLevelDomains` object is responsible for top level domain resolution
392392
**THIS EXAMPLE ILLUSTRATES HOW THE OBJECT WORK BUT SHOULD BE AVOIDED IN PRODUCTON**
393393

394394
~~~php
395-
$pdp_url = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
396-
$tldCollection = Pdp\TopLevelDomains::createFromPath($pdp_url);
395+
use Pdp\TopLevelDomains;
397396

398-
$result = $tldCollection->contains('be'); // resolution is done against the retrieves list
399-
400-
$domain = $tldCollection->resolve('www.Ulb.Ac.BE');
397+
$pdp_url = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';
398+
$tlds = TopLevelDomains::createFromPath($pdp_url);
399+
$result = $tlds->contains('be'); // resolution is done against the retrieves list
400+
$domain = json_encode($tlds->resolve('www.Ulb.Ac.BE'), JSON_PRETTY_PRINT);
401401
// returns
402402
// {
403403
// "domain": "www.ulb.ac.be",
@@ -477,8 +477,6 @@ The package comes bundle with:
477477
#### Refreshing the cached PSL and TLD data
478478

479479
~~~php
480-
<?php
481-
482480
public Manager::refreshRules(string $source_url = self::PSL_URL, $ttl = null): bool
483481
public Manager::refreshTLD(string $source_url = self::RZD_URL, $ttl = null): bool
484482
~~~
@@ -500,17 +498,15 @@ if ($retval) {
500498
#### Returning `Pdp\Rules` and `Pdp\TopLevelDomains` objects
501499

502500
~~~php
503-
<?php
504-
505501
public Manager::getRules(
506-
string $source_url = self::PSL_URL,
502+
string $url = self::PSL_URL,
507503
$ttl = null,
508504
int $asciiIDNAOption = IDNA_DEFAULT,
509505
int $unicodeIDNAOption = IDNA_DEFAULT
510506
): Rules
511507

512508
public Manager::getTLDs(
513-
string $source_url = self::RZD_URL,
509+
string $url = self::RZD_URL,
514510
$ttl = null,
515511
int $asciiIDNAOption = IDNA_DEFAULT,
516512
int $unicodeIDNAOption = IDNA_DEFAULT
@@ -519,14 +515,14 @@ public Manager::getTLDs(
519515

520516
These methods returns a `Pdp\Rules` or `Pdp\TopLevelDomains` objects seeded with their corresponding data fetch from the cache or from the external resources depending on the submitted `$ttl` argument.
521517

522-
These methods take an optional `$source_url` argument which specifies the PSL source URL. If no local cache exists for the submitted source URL, the method will:
518+
These methods take an optional `$url` argument which specifies the PSL source URL. If no local cache exists for the submitted source URL, the method will:
523519

524520
1. call `Manager::refreshRules` with the given URL and `$ttl` argument to update its local cache
525521
2. instantiate the `Rules` or the `TopLevelDomains` objects with the newly cached data.
526522

527523
On error, theses methods will throw an `Pdp\Exception`.
528524

529-
**since 5.5***
525+
**since version 5.5**
530526

531527
the following optional arguments are added to the methods:
532528

@@ -540,8 +536,6 @@ They are used when instantiated the returned object.
540536
**THIS IS THE RECOMMENDED WAY OF USING THE LIBRARY**
541537

542538
~~~php
543-
<?php
544-
545539
$manager = new Pdp\Manager(new Pdp\Cache(), new Pdp\CurlHttpClient());
546540
$tldCollection = $manager->getTLDs(self::RZD_URL);
547541
$domain = $tldCollection->resolve('www.ulb.ac.be');

src/Cache.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use function is_object;
3535
use function is_writable;
3636
use function mkdir;
37+
use function preg_match;
3738
use function realpath;
3839
use function rename;
3940
use function sprintf;
@@ -315,7 +316,7 @@ private function validateKey($key)
315316
throw new CacheException(sprintf('Expected key to be a string; received "%s"', is_object($key) ? get_class($key) : gettype($key)));
316317
}
317318

318-
if (preg_match(self::PSR16_RESERVED, $key, $match) === 1) {
319+
if (1 === preg_match(self::PSR16_RESERVED, $key, $match)) {
319320
throw new CacheException(sprintf('invalid character in key: %s', $match[0]));
320321
}
321322
}

src/Converter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function convert(string $content): array
8989
*/
9090
private function getSection(string $section, string $line): string
9191
{
92-
if (preg_match(self::REGEX_PSL_SECTION, $line, $matches)) {
92+
if (1 === preg_match(self::REGEX_PSL_SECTION, $line, $matches)) {
9393
return self::PSL_SECTION[$matches['type']][$matches['point']];
9494
}
9595

src/Domain.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private function normalize(PublicSuffix $subject): PublicSuffix
184184
return $subject;
185185
}
186186

187-
if (!preg_match(self::REGEXP_IDN_PATTERN, $this->domain)) {
187+
if (1 !== preg_match(self::REGEXP_IDN_PATTERN, $this->domain)) {
188188
return $subject->toAscii();
189189
}
190190

@@ -637,7 +637,7 @@ private function normalizeContent($domain)
637637
return $domain;
638638
}
639639

640-
if (preg_match(self::REGEXP_IDN_PATTERN, $this->domain)) {
640+
if (1 === preg_match(self::REGEXP_IDN_PATTERN, $this->domain)) {
641641
return $this->idnToUnicode($domain, $this->unicodeIDNAOption);
642642
}
643643

src/TLDConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function convert(
9292
*/
9393
private function extractHeader(string $content): array
9494
{
95-
if (!preg_match('/^\# Version (?<version>\d+), Last Updated (?<date>.*?)$/', $content, $matches)) {
95+
if (1 !== preg_match('/^\# Version (?<version>\d+), Last Updated (?<date>.*?)$/', $content, $matches)) {
9696
throw new CouldNotLoadTLDs(sprintf('Invalid Version line: %s', $content));
9797
}
9898

0 commit comments

Comments
 (0)