Passed
Push — master ( a23672...3dee81 )
by Iman
08:02
created

src/Utils/Normalizers/CacheTagsNormalizer.php (2 issues)

1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class CacheTagsNormalizer implements NormalizerContract
8
{
9
    /**
10
     * ّFigures out what the cache tags should be.
11
     * @param object $widget
12
     * @return void
13
     */
14 16
    public function normalize($widget): void
15
    {
16 16
        if (! property_exists($widget, 'cacheTags')) {
17 15
            $widget->cacheTags = [];
18 1
        } elseif (is_array($widget->cacheTags)) {
19 1
            $this->checkTagForString($widget);
20
        } else {
21
            $this->errorOut($widget);
22
        }
23 16
    }
24
25
    /**
26
     * @param $widget
27
     * @param null $tag
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tag is correct as it would always require null to be passed?
Loading history...
28
     */
29
    private function errorOut($widget, $tag = null): void
30
    {
31
        $tag = $tag ? ' '.$tag.'is not string' : '';
0 ignored issues
show
$tag is of type null, thus it always evaluated to false.
Loading history...
32
        throw new \InvalidArgumentException('Cache Tags on "'.get_class($widget).'" must be of type Array with String elements.'.$tag);
33
    }
34
35
    /**
36
     * @param $widget
37
     */
38 1
    protected function checkTagForString($widget): void
39
    {
40 1
        foreach ($widget->cacheTags as $tag) {
41 1
            if (! is_string($tag)) {
42 1
                $this->errorOut($widget);
43
            }
44
        }
45 1
    }
46
}
47