Test Failed
Pull Request — master (#37)
by Divine Niiquaye
12:44
created

DeprecationTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 20
c 2
b 0
f 0
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A triggerDeprecation() 0 11 3
A isDeprecated() 0 3 1
A getDeprecation() 0 7 2
A deprecate() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Definitions\Traits;
19
20
use PhpParser\BuilderFactory;
21
22
/**
23
 * This trait adds deprecation functionality to the service definition.
24
 *
25
 * @author Divine Niiquaye Ibok <[email protected]>
26
 */
27
trait DeprecationTrait
28
{
29
    /** @var array<string,string> */
30
    private array $deprecation = [];
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function deprecate(string $package = '', float $version = null, string $message = null)
36
    {
37
        $this->deprecation['package'] = $package;
38
        $this->deprecation['version'] = $version ?? '';
39
40
        if (!empty($message)) {
41
            if (\preg_match('#[\r\n]|\*/#', $message)) {
42
                throw new \InvalidArgumentException('Invalid characters found in deprecation template.');
43
            }
44
45
            if (!\str_contains($message, '%service_id%')) {
46
                throw new \InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
47
            }
48
        }
49
50
        $this->deprecation['message'] = $message ?? 'The "%service_id%" service is deprecated. avoid using it, as it will be removed in the future.';
51
52
        return $this;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isDeprecated(): bool
59
    {
60
        return !empty($this->deprecation);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getDeprecation(string $id): array
67
    {
68
        if (isset($this->deprecation['message'])) {
69
            $this->deprecation['message'] = \str_replace('%service_id%', $id, $this->deprecation['message']);
70
        }
71
72
        return $this->deprecation;
73
    }
74
75
    /**
76
     * Triggers a silenced deprecation notice.
77
     *
78
     * @param string $id
79
     * @param BuilderFactory|null $builder
80
     *
81
     * @return \PhpParser\Node\Expr\FuncCall|null
82
     */
83
    public function triggerDeprecation(string $id, ?BuilderFactory $builder = null)
84
    {
85
        if ([] !== $deprecation = $this->getDeprecation($id)) {
86
            \trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']);
87
88
            if (null !== $builder) {
89
                return $builder->funcCall('\trigger_deprecation', \array_values($deprecation));
90
            }
91
        }
92
93
        return null;
94
    }
95
}
96