Issues (13)

src/Relation/HasManyThrough.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura project for PHP.
5
 *
6
 * @package Aura.Marshal
7
 *
8
 * @license https://opensource.org/licenses/mit-license.php MIT
9
 *
10
 */
11
namespace Aura\Marshal\Relation;
12
13
use Aura\Marshal\Collection\GenericCollection;
14
use Aura\Marshal\Entity\GenericEntity;
15
use Aura\Marshal\Exception;
16
use Aura\Marshal\Manager;
17
use Aura\Marshal\Type\GenericType;
18
19
/**
20
 *
21
 * Represents a relationship where a native entity has many foreign entities
22
 * (i.e., a foreign collection) mapped through an association type.
23
 *
24
 * @package Aura.Marshal
25
 *
26
 */
27
class HasManyThrough extends AbstractRelation implements RelationInterface
28
{
29
    /**
30
     *
31
     * Returns the related foreign collection for a native entity.
32
     *
33
     * @param mixed $entity The native entity.
34
     *
35
     * @return GenericCollection
36
     *
37
     */
38
    public function getForEntity($entity)
39
    {
40
        if (isset($this->through, $this->through_native_field, $this->through_foreign_field)) {
41
            // first, find the native values in the through type
42
            $native_field = $this->native_field;
43
            $native_value = $entity->$native_field;
44
            $through_coll = $this->through->getCollectionByField(
0 ignored issues
show
The method getCollectionByField() 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

44
            /** @scrutinizer ignore-call */ 
45
            $through_coll = $this->through->getCollectionByField(

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...
45
                $this->through_native_field,
46
                $native_value
47
            );
48
    
49
            // now find the foreign values from the through collection
50
            $foreign_values = $through_coll->getFieldValues(
51
                $this->through_foreign_field
52
            );
53
        }
54
55
        // finally, return a foreign collection based on the foreign values
56
        return $this->foreign->getCollectionByField(
57
            $this->foreign_field,
58
            $foreign_values ?? []
59
        );
60
    }
61
}
62