Passed
Push — master ( e5acb2...2b13b1 )
by Evgeny
07:45
created

LocatorAwareTrait::getTableLocator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Locator;
13
14
use CakeDC\Api\Service\ServiceRegistry;
15
16
/**
17
 * Contains method for setting and accessing LocatorInterface instance
18
 */
19
trait LocatorAwareTrait
20
{
21
22
    /**
23
     * Service locator instance
24
     *
25
     * @var \CakeDC\Api\Service\Locator\LocatorInterface
26
     */
27
    protected $_serviceLocator;
28
29
    /**
30
     * Sets the service locator.
31
     * If no parameters are passed, it will return the currently used locator.
32
     *
33
     * @param \CakeDC\Api\Service\Locator\LocatorInterface|null $serviceLocator LocatorInterface instance.
34
     * @return \CakeDC\Api\Service\Locator\LocatorInterface
35
     * @deprecated 3.6.0 Use getTableLocator()/setTableLocator() instead.
36
     */
37
    public function serviceLocator(LocatorInterface $serviceLocator = null)
0 ignored issues
show
Unused Code introduced by
The parameter $serviceLocator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        deprecationWarning(
40
            get_called_class() . '::tableLocator() is deprecated. ' .
41
            'Use getTableLocator()/setTableLocator() instead.'
42
        );
43
        if ($tableLocator !== null) {
44
            $this->setTableLocator($tableLocator);
0 ignored issues
show
Bug introduced by
The variable $tableLocator does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
45
        }
46
47
        return $this->getTableLocator();
48
    }
49
50
    /**
51
     * Sets the table locator.
52
     *
53
     * @param \Cake\ORM\Locator\LocatorInterface $tableLocator LocatorInterface instance.
54
     * @return $this
55
     */
56
    public function setTableLocator(LocatorInterface $tableLocator)
57
    {
58
        $this->_tableLocator = $tableLocator;
0 ignored issues
show
Bug introduced by
The property _tableLocator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
60
        return $this;
61
    }
62
63
    /**
64
     * Gets the table locator.
65
     *
66
     * @return \Cake\ORM\Locator\LocatorInterface
67
     */
68
    public function getTableLocator()
69
    {
70
        if (!$this->_tableLocator) {
71
            $this->_tableLocator = TableRegistry::getTableLocator();
72
        }
73
74
        return $this->_tableLocator;
75
    }
76
}
77