HasMany   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 28
ccs 0
cts 13
cp 0
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A hasMany() 0 17 3
1
<?php
2
3
namespace Ronanchilvers\Orm\Features\Relationship;
4
5
use ReflectionClass;
6
use Ronanchilvers\Orm\Orm;
7
use Ronanchilvers\Utility\Str;
8
use RuntimeException;
9
10
/**
11
 * Has Many relationship - one to many, implies possesion
12
 *
13
 * Eg: A user has many bookmarks, a forest has many trees
14
 *
15
 * @author Ronan Chilvers <[email protected]>
16
 */
17
trait HasMany
18
{
19
    /**
20
     * Get the data for a 'belongs to' relationship
21
     *
22
     * @param string $modelClass The model class that we 'belong to'
23
     * @param string $attribute The local data attribute that identifies the 'belongs to' key
24
     * @param string $foreignAttribute The foreign data attribute that the local field references
25
     * @return \Ronanchilvers\Orm\Model
26
     * @author Ronan Chilvers <[email protected]>
27
     */
28
    protected function hasMany(string $modelClass, string $foreignAttribute = null, string $attribute = 'id')
29
    {
30
        if (!class_exists($modelClass)) {
31
            throw new RuntimeException('Invalid model class in belongs_to definition');
32
        }
33
        if (is_null($foreignAttribute)) {
34
            $reflection = new ReflectionClass(get_called_class());
35
            $foreignAttribute = Str::snake($reflection->getShortName());
36
            $foreignAttribute = strtolower($foreignAttribute);
37
            $foreignAttribute = $modelClass::prefix($foreignAttribute);
38
        }
39
        $attribute = static::prefix($attribute);
40
        $finder = Orm::finder($modelClass);
41
42
        return $finder->select()
43
                        ->where($foreignAttribute, $this->getAttribute($attribute))
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

43
                        ->where($foreignAttribute, $this->/** @scrutinizer ignore-call */ getAttribute($attribute))
Loading history...
44
                        ->execute();
45
    }
46
}
47