AbstractCollection::handleMappedBy()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.2
cc 4
eloc 7
nc 5
nop 2
1
<?php
2
3
namespace Saxulum\Accessor\Accessors;
4
5
use Doctrine\Common\Collections\Collection;
6
use Saxulum\Accessor\CallbackBag;
7
use Saxulum\Accessor\Collection\ArrayCollection;
8
use Saxulum\Accessor\Collection\CollectionInterface;
9
use Saxulum\Accessor\Collection\DoctrineArrayCollection;
10
use Saxulum\Accessor\Prop;
11
12
abstract class AbstractCollection extends AbstractWrite
13
{
14
    /**
15
     * @param CallbackBag $callbackBag
16
     */
17
    protected function propertyDefault(CallbackBag $callbackBag)
18
    {
19
        if (null === $callbackBag->getProperty()) {
20
            $callbackBag->setProperty(array());
21
        }
22
    }
23
24
    /**
25
     * @param  CallbackBag         $callbackBag
26
     * @return CollectionInterface
27
     */
28
    protected function getCollection(CallbackBag $callbackBag)
29
    {
30
        if (is_array($callbackBag->getProperty())) {
31
            return new ArrayCollection($callbackBag->getProperty());
32
        }
33
34
        if (interface_exists('Doctrine\Common\Collections\Collection') && $callbackBag->getProperty() instanceof Collection) {
35
            return new DoctrineArrayCollection($callbackBag->getProperty());
36
        }
37
38
        throw new \InvalidArgumentException("Property must be an array or a collection!");
39
    }
40
41
    /**
42
     * @param  CallbackBag $callbackBag
43
     * @param  bool        $nullValue
44
     * @throws \Exception
45
     */
46
    protected function handleMappedBy(CallbackBag $callbackBag, $nullValue = false)
47
    {
48
        if (null === $mappedBy = $callbackBag->getMappedBy()) {
49
            throw new \Exception("Remote name needs to be set on '{$callbackBag->getName()}', if remote type is given!");
50
        }
51
52
        $value = false === $nullValue ? $callbackBag->getObject() : null;
53
54
        if (false === $callbackBag->getArgument(1, false)) {
55
            $method = $this->getPrefixByProp($callbackBag->getProp()) . ucfirst($mappedBy);
56
            $callbackBag->getArgument(0)->$method($value, true);
0 ignored issues
show
Bug introduced by
The method $method cannot be called on $callbackBag->getArgument(0) (of type false).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
        }
58
    }
59
60
    /**
61
     * @param  Prop   $prop
62
     * @param  string $namespace
63
     * @return string
64
     */
65
    protected static function getPhpDocHint(Prop $prop, $namespace)
66
    {
67
        $hint = $prop->getPhpDocHint($namespace);
68
69
        if ('' === $hint) {
70
            return $hint;
71
        }
72
73
        if ('[]' === substr($hint, -2)) {
74
            return substr($hint, 0, -2) . ' ';
75
        }
76
77
        return $hint . ' ';
78
    }
79
}
80