Passed
Pull Request — master (#993)
by Maxim
20:44 queued 10:42
created

ContainerDirective   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 14
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A renderInject() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Stempler\Directive;
6
7
use Spiral\Stempler\Exception\DirectiveException;
8
use Spiral\Stempler\Node\Dynamic\Directive;
9
10
/**
11
 * Provides the ability to inject services into view code. Can only be used within view object.
12
 */
13
final class ContainerDirective extends AbstractDirective
14
{
15
    /**
16
     * Injects service into template.
17
     */
18 3
    public function renderInject(Directive $directive): string
19
    {
20 3
        if (\count($directive->values) < 2 || (string) $directive->values[0] === '') {
21 1
            throw new DirectiveException(
22 1
                'Unable to call @inject directive, 2 values required',
23 1
                $directive->getContext()
0 ignored issues
show
Bug introduced by
It seems like $directive->getContext() can also be of type null; however, parameter $context of Spiral\Stempler\Exceptio...xception::__construct() does only seem to accept Spiral\Stempler\Parser\Context, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
                /** @scrutinizer ignore-type */ $directive->getContext()
Loading history...
24 1
            );
25
        }
26
27 2
        if ($directive->values[0][0] === '$') {
28 1
            return \sprintf(
29 1
                '<?php %s = $this->container->get(%s); ?>',
30 1
                $directive->values[0],
31 1
                $directive->values[1]
32 1
            );
33
        }
34
35 1
        return \sprintf(
36 1
            '<?php $%s = $this->container->get(%s); ?>',
37 1
            \trim($directive->values[0], '\'"'),
38 1
            $directive->values[1]
39 1
        );
40
    }
41
}
42