TemplateNormalizer::normalize()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class TemplateNormalizer implements NormalizerContract
8
{
9
    /**
10
     * Figures out which template to render.
11
     *
12
     * @param  object  $widget
13
     * @return void
14
     */
15 24
    public function normalize($widget): void
16
    {
17
        // class name without namespace.
18 24
        $className = str_replace(app()->getNamespace().'Widgets\\', '', get_class($widget));
0 ignored issues
show
introduced by
The method getNamespace() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

18
        $className = str_replace(app()->/** @scrutinizer ignore-call */ getNamespace().'Widgets\\', '', get_class($widget));
Loading history...
19
20
        // replace slashes with dots
21 24
        $className = str_replace(['\\', '/'], '.', $className);
22
23 24
        if (! property_exists($widget, 'template')) {
24 1
            $widget->template = 'Widgets::'.$className.'View';
25
        }
26
27 24
        if (! view()->exists($widget->template)) {
28
            throw new \InvalidArgumentException("View file \"{$widget->template}\" not found by: '".get_class($widget)." '");
29
        }
30 24
    }
31
}
32