Completed
Push — master ( 49d7ca...633ddc )
by Peter
05:46
created

MaxScoreAwareTrait::getMaxScore()   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 0
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link http://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Traits;
14
15
use Maslosoft\Manganel\Interfaces\MaxScoreAwareInterface;
16
use UnexpectedValueException;
17
18
/**
19
 * MaxScoreAwareTrait
20
 *
21
 * @see MaxScoreAwareInterface
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
trait MaxScoreAwareTrait
25
{
26
27
	private $maxScore = 0.0;
28
29 1
	public function getMaxScore()
30
	{
31 1
		if (!$this instanceof MaxScoreAwareInterface)
32
		{
33
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, MaxScoreAwareInterface::class)); // @codeCoverageIgnore
34
		}
35 1
		return $this->maxScore;
1 ignored issue
show
Bug introduced by
Accessing maxScore on the interface Maslosoft\Manganel\Inter...\MaxScoreAwareInterface 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...
36
	}
37
38 1
	public function setMaxScore($score)
39
	{
40 1
		if (!$this instanceof MaxScoreAwareInterface)
41
		{
42
			throw new UnexpectedValueException(sprintf('Class `%s` using `%s` must implement `%s`', get_class($this), __CLASS__, MaxScoreAwareInterface::class)); // @codeCoverageIgnore
43
		}
44 1
		$this->maxScore = floatval($score);
1 ignored issue
show
Bug introduced by
Accessing maxScore on the interface Maslosoft\Manganel\Inter...\MaxScoreAwareInterface 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...
45 1
		return $this;
46
	}
47
48
}
49