Completed
Push — master ( 91a3af...095fa7 )
by Peter
06:05
created

IndexAwareTrait::getIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Manganel\Traits;
10
11
use Maslosoft\Manganel\Interfaces\IndexAwareInterface;
12
use UnexpectedValueException;
13
14
/**
15
 * IndexAwareTrait
16
 *
17
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
18
 */
19
trait IndexAwareTrait
20
{
21
22
	private $index = '';
23
24
	/**
25
	 * Get currently used index
26
	 * @return string
27
	 */
28 1
	public function getIndex()
29
	{
30 1
		if (!$this instanceof IndexAwareInterface)
31
		{
32
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, IndexAwareInterface::class)); // @codeCoverageIgnore
33
		}
34 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...
35
	}
36
37
	/**
38
	 * Set currently used index
39
	 * @param string $index
40
	 * @return static
41
	 */
42 1
	public function setIndex($index)
43
	{
44 1
		if (!$this instanceof IndexAwareInterface)
45
		{
46
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, IndexAwareInterface::class)); // @codeCoverageIgnore
47
		}
48 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...
49 1
		return $this;
50
	}
51
52
}
53