Completed
Push — master ( 4be8f9...c3a216 )
by Randy
01:42
created

ObjectEnsurance   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 18.67 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 14
loc 75
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A isSome() 0 6 1
A isNotSome() 0 6 1
A isInstanceOf() 7 7 2
A isNotInstanceOf() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use Dgame\Ensurance\Exception\EnsuranceException;
6
7
/**
8
 * Class ObjectEnsurance
9
 * @package Dgame\Ensurance
10
 */
11
final class ObjectEnsurance extends ClassEnsurance
12
{
13
    /**
14
     * @var object
15
     */
16
    private $object;
17
18
    /**
19
     * ObjectEnsurance constructor.
20
     *
21
     * @param $object
22
     *
23
     * @throws EnsuranceException
24
     */
25
    public function __construct($object)
26
    {
27
        if (!is_object($object)) {
28
            throw new EnsuranceException('That is not an object');
29
        }
30
31
        parent::__construct(get_class($object));
32
33
        $this->object = $object;
34
    }
35
36
    /**
37
     * @param string $class
38
     *
39
     * @return ObjectEnsurance
40
     */
41
    public function isSome(string $class): ObjectEnsurance
42
    {
43
        $this->enforce(is_a($this->object, $class))->orThrow('"%s" is not "%s"', get_class($this->object), $class);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $class
50
     *
51
     * @return ObjectEnsurance
52
     */
53
    public function isNotSome(string $class): ObjectEnsurance
54
    {
55
        $this->enforce(!is_a($this->object, $class))->orThrow('"%s" is "%s"', get_class($this->object), $class);
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param $class
62
     *
63
     * @return ObjectEnsurance
64
     */
65 View Code Duplication
    public function isInstanceOf($class): ObjectEnsurance
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $class = is_object($class) ? get_class($class) : $class;
68
        $this->enforce($this->object instanceof $class)->orThrow('"%s" is not an instance of "%s"', get_class($this->object), $class);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @param $class
75
     *
76
     * @return ObjectEnsurance
77
     */
78 View Code Duplication
    public function isNotInstanceOf($class): ObjectEnsurance
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $class = is_object($class) ? get_class($class) : $class;
81
        $this->enforce(!($this->object instanceof $class))->orThrow('"%s" is an instance of "%s"', get_class($this->object), $class);
82
83
        return $this;
84
    }
85
}