Completed
Push — master ( 3fab57...455a7d )
by Peter
14:19
created

IndexAwareTrait::setIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Traits;
14
15
use Maslosoft\Manganel\Interfaces\IndexAwareInterface;
16
use UnexpectedValueException;
17
18
/**
19
 * IndexAwareTrait
20
 *
21
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
22
 */
23
trait IndexAwareTrait
24
{
25
26
	private $index = '';
27
28
	/**
29
	 * Get currently used index
30
	 * @return string
31
	 */
32 1
	public function getIndex()
33
	{
34 1
		if (!$this instanceof IndexAwareInterface)
35
		{
36
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, IndexAwareInterface::class)); // @codeCoverageIgnore
37
		}
38 1
		return $this->index;
0 ignored issues
show
Bug introduced by
Accessing index on the interface Maslosoft\Manganel\Interfaces\IndexAwareInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
39
	}
40
41
	/**
42
	 * Set currently used index
43
	 * @param string $index
44
	 * @return static
45
	 */
46 1
	public function setIndex($index)
47
	{
48 1
		if (!$this instanceof IndexAwareInterface)
49
		{
50
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, IndexAwareInterface::class)); // @codeCoverageIgnore
51
		}
52 1
		$this->index = $index;
0 ignored issues
show
Bug introduced by
Accessing index on the interface Maslosoft\Manganel\Interfaces\IndexAwareInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
53 1
		return $this;
54
	}
55
56
}
57