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

IndexAwareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 34
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndex() 0 8 2
A setIndex() 0 9 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