Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

ViewPath::get()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
ccs 16
cts 17
cp 0.9412
rs 8.4444
cc 8
nc 40
nop 0
crap 8.013
1
<?php
2
3
namespace Thinktomorrow\Chief\Concerns\Viewable;
4
5
use Thinktomorrow\Chief\Relations\ActsAsParent;
6
use Thinktomorrow\Chief\Sets\Set;
7
8
class ViewPath
9
{
10
    /** @var ViewableContract */
11
    private $viewable;
12
13
    /** @var ActsAsParent */
14
    private $parent;
15
16
    /** @var string */
17
    private $viewBasePath;
18
19 11
    private function __construct(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null)
20
    {
21 11
        $this->viewable = $viewable;
22 11
        $this->parent = $parent;
23 11
        $this->viewBasePath = $viewBasePath;
24 11
    }
25
26 11
    public static function make(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null)
27
    {
28 11
        return new static($viewable, $parent, $viewBasePath);
29
    }
30
31
    /**
32
     * @return string
33
     * @throws NotFoundView
34
     */
35 11
    public function get(): string
36
    {
37 11
        $basePath = $this->viewBasePath ? $this->viewBasePath . '.' : '';
38 11
        $guessedParentViewName = $this->parent ? $this->parent->viewKey() : '';
0 ignored issues
show
Bug introduced by
The method viewKey() does not exist on Thinktomorrow\Chief\Relations\ActsAsParent. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Relations\ActsAsParent. ( Ignorable by Annotation )

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

38
        $guessedParentViewName = $this->parent ? $this->parent->/** @scrutinizer ignore-call */ viewKey() : '';
Loading history...
39 11
        $guessedViewName = $this->viewable->viewKey();
40
41
        $viewPaths = [
42 11
            $basePath.$guessedParentViewName.'.'.$guessedViewName,
43 11
            $basePath.$guessedViewName,
44 11
            $basePath.$guessedViewName.'.show',
45 11
            $basePath.'show',
46
        ];
47
48
        // In case the collection set is made out of pages, we'll also allow to use the
49
        // generic collection page view for these sets as well as a fallback view
50 11
        if ($this->viewable instanceof Set && $this->viewable->first() instanceof ViewableContract) {
51 6
            $viewPaths[] = $basePath.$guessedParentViewName.'.'.$this->viewable->first()->viewKey();
52 6
            $viewPaths[] = $basePath.$this->viewable->first()->viewKey();
53
        }
54
55 11
        foreach ($viewPaths as $viewPath) {
56 11
            if (! view()->exists($viewPath)) {
57 11
                continue;
58
            }
59
60
            return $viewPath;
61
        }
62
63 11
        if (! view()->exists($basePath.'show')) {
64 11
            throw new NotFoundView('Viewfile not found for ['.get_class($this->viewable).']. Make sure the view ['.$basePath.$guessedViewName.'] or the default view ['.$basePath.'show] exists.');
65
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 63 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
66
    }
67
}
68