Test Failed
Push — master ( fdb79d...2e4512 )
by Chris
19:35
created

TaxonomyRegistrar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Leonidas\Library\System\Taxonomy;
4
5
use Leonidas\Contracts\System\Taxonomy\TaxonomyInterface;
6
use Leonidas\Contracts\System\Taxonomy\TaxonomyOptionHandlerCollectionInterface;
7
use Leonidas\Contracts\System\Taxonomy\TaxonomyRegistrarInterface;
8
use Leonidas\Library\System\AbstractSystemModelTypeRegistrar;
9
10
class TaxonomyRegistrar extends AbstractSystemModelTypeRegistrar implements TaxonomyRegistrarInterface
11
{
12
    protected ?TaxonomyOptionHandlerCollectionInterface $optionHandlers = null;
13
14
    public function __construct(?TaxonomyOptionHandlerCollectionInterface $optionHandlers = null)
15
    {
16
        $this->optionHandlers = $optionHandlers;
17
    }
18
19
    public function registerOne(TaxonomyInterface $taxonomy)
20
    {
21
        $registered = register_taxonomy(
0 ignored issues
show
Bug introduced by
The function register_taxonomy was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

21
        $registered = /** @scrutinizer ignore-call */ register_taxonomy(
Loading history...
22
            $name = $taxonomy->getName(),
23
            $types = $taxonomy->getObjectTypes(),
24
            $this->getArgs($taxonomy)
25
        );
26
27
        foreach ($types as $type) {
28
            register_taxonomy_for_object_type($name, $type);
0 ignored issues
show
Bug introduced by
The function register_taxonomy_for_object_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

28
            /** @scrutinizer ignore-call */ 
29
            register_taxonomy_for_object_type($name, $type);
Loading history...
29
        }
30
31
        $registered->options = $taxonomy->getOptions();
32
33
        if (isset($this->optionHandlers)) {
34
            $this->registerOptions($taxonomy);
35
        }
36
    }
37
38
    public function registerMany(TaxonomyInterface ...$taxonomies)
39
    {
40
        foreach ($taxonomies as $taxonomy) {
41
            $this->registerOne($taxonomy);
42
        }
43
    }
44
45
    protected function getArgs(TaxonomyInterface $taxonomy)
46
    {
47
        $args = [
48
            "show_tagcloud" => $taxonomy->isShownInTagCloud(),
49
            "show_in_quick_edit" => $taxonomy->isShownInQuickEdit(),
50
            "show_admin_column" => $taxonomy->showsAdminColumn(),
51
            "meta_box_cb" => $taxonomy->getMetaBoxCb(),
52
            "meta_box_sanitize_cb" => $taxonomy->getMetaBoxSanitizeCb(),
53
            "update_count_callback" => $taxonomy->getUpdateCountCallback(),
54
            "default_term" => $taxonomy->getDefaultTerm(),
55
            "sort" => $taxonomy->shouldBeSorted(),
56
            "args" => $taxonomy->getArgs(),
57
        ] + $this->getBaseArgs($taxonomy);
58
59
        return array_filter($args, fn ($arg) => $arg !== null);
60
    }
61
62
    protected function registerOptions(TaxonomyInterface $taxonomy)
63
    {
64
        foreach ($taxonomy->getOptions() as $option => $value) {
65
            if ($this->optionHandlers->has($option)) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

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

65
            if ($this->optionHandlers->/** @scrutinizer ignore-call */ has($option)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
                $this->optionHandlers->get($option)->handle($taxonomy, $value);
67
            }
68
69
            throw $this->unregisteredOptionException($option);
70
        }
71
    }
72
}
73