Skip to content

Commit 1865f64

Browse files
committed
Update source code to the new development requirements
1 parent edf65fd commit 1865f64

13 files changed

+141
-114
lines changed

src/Cache.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616
namespace Pdp;
1717

1818
use DateInterval;
19+
use DateTimeImmutable;
1920
use FilesystemIterator;
2021
use Generator;
2122
use InvalidArgumentException;
2223
use Psr\SimpleCache\CacheInterface;
23-
use Traversable;
2424
use function chmod;
25-
use function date_create_immutable;
2625
use function dirname;
2726
use function file_exists;
2827
use function file_get_contents;
2928
use function filemtime;
3029
use function get_class;
3130
use function gettype;
32-
use function is_array;
3331
use function is_int;
32+
use function is_iterable;
3433
use function is_object;
3534
use function is_writable;
3635
use function mkdir;
@@ -83,6 +82,7 @@ final class Cache implements CacheInterface
8382
public function __construct(string $cache_path = '')
8483
{
8584
if ('' === $cache_path) {
85+
/** @var string $cache_path */
8686
$cache_path = realpath(dirname(__DIR__).DIRECTORY_SEPARATOR.'data');
8787
}
8888

@@ -182,7 +182,9 @@ private function getExpireAt($ttl): int
182182
}
183183

184184
if ($ttl instanceof DateInterval) {
185-
return date_create_immutable('@'.time())->add($ttl)->getTimestamp();
185+
$now = new DateTimeImmutable('@'.time());
186+
187+
return $now->add($ttl)->getTimestamp();
186188
}
187189

188190
throw new CacheException(sprintf('Expected TTL to be an int, a DateInterval or null; received "%s"', is_object($ttl) ? get_class($ttl) : gettype($ttl)));
@@ -218,13 +220,13 @@ public function clear()
218220
*/
219221
public function getMultiple($keys, $default = null)
220222
{
221-
if (!is_array($keys) && !$keys instanceof Traversable) {
223+
if (!is_iterable($keys)) {
222224
throw new CacheException('keys must be either of type array or Traversable');
223225
}
224226

225227
$values = [];
226228
foreach ($keys as $key) {
227-
$values[$key] = $this->get($key) ?: $default;
229+
$values[$key] = $this->get($key) ?? $default;
228230
}
229231

230232
return $values;
@@ -235,7 +237,7 @@ public function getMultiple($keys, $default = null)
235237
*/
236238
public function setMultiple($values, $ttl = null)
237239
{
238-
if (!is_array($values) && !$values instanceof Traversable) {
240+
if (!is_iterable($values)) {
239241
throw new CacheException('keys must be either of type array or Traversable');
240242
}
241243

@@ -252,22 +254,28 @@ public function setMultiple($values, $ttl = null)
252254
/**
253255
* {@inheritdoc}
254256
*/
255-
public function deleteMultiple($keys)
257+
public function deleteMultiple($keys): bool
256258
{
257-
if (!is_array($keys) && !$keys instanceof Traversable) {
259+
if (!is_iterable($keys)) {
258260
throw new CacheException('keys must be either of type array or Traversable');
259261
}
260262

263+
$successful = false;
261264
foreach ($keys as $key) {
262265
$this->validateKey($key);
263-
$this->delete($key);
266+
$successful = $this->delete($key);
267+
if (false === $successful) {
268+
break;
269+
}
264270
}
271+
272+
return $successful;
265273
}
266274

267275
/**
268276
* {@inheritdoc}
269277
*/
270-
public function has($key)
278+
public function has($key): bool
271279
{
272280
return $this->get($key, $this) !== $this;
273281
}
@@ -288,16 +296,14 @@ private function getPath($key): string
288296
return $this->cache_path.DIRECTORY_SEPARATOR.self::FILE_PREFIX.$key.self::FILE_EXTENSION;
289297
}
290298

291-
/**
292-
* @return Generator|string[]
293-
*/
294299
private function listPaths(): Generator
295300
{
296301
$iterator = new FilesystemIterator(
297302
$this->cache_path,
298303
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
299304
);
300305

306+
/** @var string $path */
301307
foreach ($iterator as $path) {
302308
if (!is_dir($path)) {
303309
yield $path;
@@ -310,7 +316,7 @@ private function listPaths(): Generator
310316
*
311317
* @throws CacheException
312318
*/
313-
private function validateKey($key)
319+
private function validateKey($key): void
314320
{
315321
if (!is_string($key)) {
316322
throw new CacheException(sprintf('Expected key to be a string; received "%s"', is_object($key) ? get_class($key) : gettype($key)));
@@ -326,7 +332,7 @@ private function validateKey($key)
326332
*
327333
* @param string $path absolute directory path
328334
*/
329-
private function mkdir($path)
335+
private function mkdir($path): void
330336
{
331337
$parent_path = dirname($path);
332338

src/Converter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function convert(string $content): array
6969
$file = new SplTempFileObject();
7070
$file->fwrite($content);
7171
$file->setFlags(SplTempFileObject::DROP_NEW_LINE | SplTempFileObject::READ_AHEAD | SplTempFileObject::SKIP_EMPTY);
72+
/** @var string $line */
7273
foreach ($file as $line) {
7374
$section = $this->getSection($section, $line);
7475
if ('' !== $section && false === strpos($line, '//')) {

src/CurlHttpClient.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ public function __construct(array $options = [])
6565
*/
6666
public function getContent(string $url): string
6767
{
68+
/** @var resource $curl */
6869
$curl = curl_init($url);
6970
curl_setopt_array($curl, $this->options);
71+
/** @var string $content */
7072
$content = curl_exec($curl);
7173
$error_code = curl_errno($curl);
7274
$error_message = curl_error($curl);

src/Domain.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,12 @@ private function setPublicSuffix(PublicSuffix $publicSuffix): PublicSuffix
155155
return $publicSuffix;
156156
}
157157

158-
if (!$this->isResolvable()) {
158+
if (null === $this->domain || !$this->isResolvable()) {
159159
throw new CouldNotResolvePublicSuffix(sprintf('The domain `%s` can not contain a public suffix', $this->domain));
160160
}
161161

162162
$publicSuffix = $this->normalize($publicSuffix);
163+
/** @var string $psContent */
163164
$psContent = $publicSuffix->getContent();
164165
if ($this->domain === $psContent) {
165166
throw new CouldNotResolvePublicSuffix(sprintf('The public suffix `%s` can not be equal to the domain name `%s`', $psContent, $this->domain));
@@ -199,6 +200,10 @@ private function normalize(PublicSuffix $subject): PublicSuffix
199200
*/
200201
private function setRegistrableDomain()
201202
{
203+
if (null === $this->domain) {
204+
return null;
205+
}
206+
202207
if (null === $this->publicSuffix->getContent()) {
203208
return null;
204209
}
@@ -216,6 +221,10 @@ private function setRegistrableDomain()
216221
*/
217222
private function setSubDomain()
218223
{
224+
if (null === $this->domain) {
225+
return null;
226+
}
227+
219228
if (null === $this->registrableDomain) {
220229
return null;
221230
}
@@ -246,15 +255,15 @@ public function getIterator()
246255
/**
247256
* {@inheritdoc}
248257
*/
249-
public function jsonSerialize()
258+
public function jsonSerialize(): array
250259
{
251260
return $this->__debugInfo();
252261
}
253262

254263
/**
255264
* {@inheritdoc}
256265
*/
257-
public function __debugInfo()
266+
public function __debugInfo(): array
258267
{
259268
return [
260269
'domain' => $this->domain,
@@ -270,23 +279,23 @@ public function __debugInfo()
270279
/**
271280
* {@inheritdoc}
272281
*/
273-
public function count()
282+
public function count(): int
274283
{
275284
return count($this->labels);
276285
}
277286

278287
/**
279288
* {@inheritdoc}
280289
*/
281-
public function getContent()
290+
public function getContent(): ?string
282291
{
283292
return $this->domain;
284293
}
285294

286295
/**
287296
* {@inheritdoc}
288297
*/
289-
public function __toString()
298+
public function __toString(): string
290299
{
291300
return (string) $this->domain;
292301
}
@@ -311,7 +320,7 @@ public function getDomain()
311320
/**
312321
* {@inheritdoc}
313322
*/
314-
public function getLabel(int $key)
323+
public function getLabel(int $key): ?string
315324
{
316325
if ($key < 0) {
317326
$key += count($this->labels);
@@ -385,7 +394,7 @@ public function isTransitionalDifferent(): bool
385394
*
386395
* @return string|null registrable domain
387396
*/
388-
public function getRegistrableDomain()
397+
public function getRegistrableDomain(): ?string
389398
{
390399
return $this->registrableDomain;
391400
}
@@ -400,7 +409,7 @@ public function getRegistrableDomain()
400409
*
401410
* @return string|null registrable domain
402411
*/
403-
public function getSubDomain()
412+
public function getSubDomain(): ?string
404413
{
405414
return $this->subDomain;
406415
}
@@ -410,7 +419,7 @@ public function getSubDomain()
410419
*
411420
* @return string|null
412421
*/
413-
public function getPublicSuffix()
422+
public function getPublicSuffix(): ?string
414423
{
415424
return $this->publicSuffix->getContent();
416425
}
@@ -426,7 +435,10 @@ public function getPublicSuffix()
426435
*/
427436
public function isResolvable(): bool
428437
{
429-
return 1 < count($this->labels) && '.' !== substr($this->domain, -1, 1);
438+
return null !== $this->domain
439+
&& '.' !== substr($this->domain, -1, 1)
440+
&& 1 < count($this->labels)
441+
;
430442
}
431443

432444
/**
@@ -462,7 +474,7 @@ public function isPrivate(): bool
462474
/**
463475
* {@inheritdoc}
464476
*/
465-
public function toAscii()
477+
public function toAscii(): self
466478
{
467479
if (null === $this->domain) {
468480
return $this;
@@ -479,7 +491,7 @@ public function toAscii()
479491
/**
480492
* {@inheritdoc}
481493
*/
482-
public function toUnicode()
494+
public function toUnicode(): self
483495
{
484496
if (null === $this->domain || false === strpos($this->domain, 'xn--')) {
485497
return $this;
@@ -537,8 +549,6 @@ public function resolve($publicSuffix): self
537549
* otherwise the public suffix content is added to or remove from the current domain.
538550
*
539551
* @param mixed $publicSuffix
540-
*
541-
* @return self
542552
*/
543553
public function withPublicSuffix($publicSuffix): self
544554
{
@@ -751,21 +761,21 @@ public function withoutLabel(int $key, int ...$keys): self
751761
{
752762
array_unshift($keys, $key);
753763
$nb_labels = count($this->labels);
754-
foreach ($keys as &$key) {
755-
if (- $nb_labels > $key || $nb_labels - 1 < $key) {
756-
throw new InvalidLabelKey(sprintf('the key `%s` is invalid', $key));
764+
foreach ($keys as &$offset) {
765+
if (- $nb_labels > $offset || $nb_labels - 1 < $offset) {
766+
throw new InvalidLabelKey(sprintf('the key `%s` is invalid', $offset));
757767
}
758768

759-
if (0 > $key) {
760-
$key += $nb_labels;
769+
if (0 > $offset) {
770+
$offset += $nb_labels;
761771
}
762772
}
763-
unset($key);
773+
unset($offset);
764774

765775
$deleted_keys = array_keys(array_count_values($keys));
766776
$labels = [];
767-
foreach ($this->labels as $key => $label) {
768-
if (!in_array($key, $deleted_keys, true)) {
777+
foreach ($this->labels as $offset => $label) {
778+
if (!in_array($offset, $deleted_keys, true)) {
769779
$labels[] = $label;
770780
}
771781
}

src/DomainInterface.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ interface DomainInterface extends Countable, IteratorAggregate
2929
{
3030
/**
3131
* Returns the domain content.
32-
*
33-
* @return string|null
3432
*/
35-
public function getContent();
33+
public function getContent(): ?string;
3634

3735
/**
3836
* Returns the domain content as a string.
39-
*
40-
* @return string
4137
*/
42-
public function __toString();
38+
public function __toString(): string;
4339

4440
/**
4541
* Retrieves a single domain label.
@@ -50,10 +46,8 @@ public function __toString();
5046
* If no label is found the submitted $key the returned value will be null.
5147
*
5248
* @param int $key the label offset
53-
*
54-
* @return string|null
5549
*/
56-
public function getLabel(int $key);
50+
public function getLabel(int $key): ?string;
5751

5852
/**
5953
* Returns the associated key for each label.
@@ -74,10 +68,8 @@ public function keys(string $label): array;
7468
* an instance with its content converted to its IDNA ASCII form
7569
*
7670
* @throws Exception if the domain can not be converted to ASCII using IDN UTS46 algorithm
77-
*
78-
* @return static
7971
*/
80-
public function toAscii();
72+
public function toAscii(): self;
8173

8274
/**
8375
* Converts the domain to its IDNA UTF8 form.
@@ -86,10 +78,8 @@ public function toAscii();
8678
* an instance with its content converted to its IDNA UTF8 form
8779
*
8880
* @throws Exception if the domain can not be converted to Unicode using IDN UTS46 algorithm
89-
*
90-
* @return static
9181
*/
92-
public function toUnicode();
82+
public function toUnicode(): self;
9383

9484

9585
/**

0 commit comments

Comments
 (0)