Passed
Push — master ( 1791d6...48fae4 )
by Anton
01:59
created

src/Relation/Morphed/MorphedHasMany.php (3 issues)

Labels
Severity
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Schema\Relation\Morphed;
10
11
use Cycle\ORM\Relation;
12
use Cycle\Schema\Registry;
13
use Cycle\Schema\Relation\RelationSchema;
14
use Cycle\Schema\Relation\Traits\FieldTrait;
15
use Cycle\Schema\Relation\Traits\MorphTrait;
16
17
class MorphedHasMany extends RelationSchema
18
{
19
    use FieldTrait, MorphTrait;
20
21
    // internal relation type
22
    protected const RELATION_TYPE = Relation::MORPHED_HAS_MANY;
23
24
    // relation schema options
25
    protected const RELATION_SCHEMA = [
26
        // nullable by default
27
        Relation::NULLABLE               => true,
28
29
        // default field name for inner key
30
        Relation::OUTER_KEY              => '{relation}_{source:primaryKey}',
31
32
        // link to parent entity primary key by default
33
        Relation::INNER_KEY              => '{source:primaryKey}',
34
35
        // link to parent entity primary key by default
36
        Relation::MORPH_KEY              => '{relation}_role',
37
38
        // rendering options
39
        RelationSchema::INDEX_CREATE     => true,
40
        RelationSchema::MORPH_KEY_LENGTH => 32
41
    ];
42
43
    /**
44
     * @param Registry $registry
45
     */
46
    public function compute(Registry $registry)
47
    {
48
        parent::compute($registry);
49
50
        $source = $registry->getEntity($this->source);
51
        $target = $registry->getEntity($this->target);
52
53
        // create target outer field
54
        $this->ensureField(
55
            $target,
56
            $this->options->get(Relation::OUTER_KEY),
57
            $this->getField($source, Relation::INNER_KEY),
58
            $this->options->get(Relation::NULLABLE)
0 ignored issues
show
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\Mo...dHasMany::ensureField() does only seem to accept boolean, 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

58
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
59
        );
60
61
        // create target outer field
62
        $this->ensureMorphField(
63
            $target,
64
            $this->options->get(Relation::MORPH_KEY),
65
            $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
0 ignored issues
show
It seems like $this->options->get(Cycl...hema::MORPH_KEY_LENGTH) can also be of type string; however, parameter $lenght of Cycle\Schema\Relation\Mo...any::ensureMorphField() does only seem to accept integer, 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

65
            /** @scrutinizer ignore-type */ $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
Loading history...
66
            $this->options->get(Relation::NULLABLE)
0 ignored issues
show
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\Mo...any::ensureMorphField() does only seem to accept boolean, 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

66
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
67
        );
68
    }
69
70
    /**
71
     * @param Registry $registry
72
     */
73
    public function render(Registry $registry)
74
    {
75
        $target = $registry->getEntity($this->target);
76
77
        $outerField = $this->getField($target, Relation::OUTER_KEY);
78
        $morphField = $this->getField($target, Relation::MORPH_KEY);
79
80
        $table = $registry->getTableSchema($target);
81
82
        if ($this->options->get(self::INDEX_CREATE)) {
83
            $table->index([$outerField->getColumn(), $morphField->getColumn()]);
84
        }
85
    }
86
}