Completed
Push — version-4 ( b58713...7d1db3 )
by
unknown
06:25
created

TypeChecker   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 73
Duplicated Lines 30.14 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 5
Bugs 3 Features 2
Metric Value
c 5
b 3
f 2
dl 22
loc 73
rs 10
wmc 22
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isString() 0 11 3
A isNumeric() 0 11 3
A isDate() 0 11 4
A isArray() 11 11 4
B isIterable() 11 11 5
A isNotification() 0 10 3

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 Fenos\Notifynder\Helpers;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use InvalidArgumentException;
8
use Traversable;
9
10
class TypeChecker
11
{
12
    public static function isString($value, $strict = true)
13
    {
14
        if (!is_string($value)) {
15
            if ($strict) {
16
                throw new InvalidArgumentException('The value passed must be a string');
17
            }
18
            return false;
19
        }
20
21
        return true;
22
    }
23
24
    public static function isNumeric($value, $strict = true)
25
    {
26
        if (!is_numeric($value)) {
27
            if ($strict) {
28
                throw new InvalidArgumentException('The value passed must be a number');
29
            }
30
            return false;
31
        }
32
33
        return true;
34
    }
35
36
    public static function isDate($value, $strict = true)
37
    {
38
        if ($value instanceof Carbon || $value instanceof DateTime) {
39
            return true;
40
        }
41
42
        if ($strict) {
43
            throw new InvalidArgumentException('The value passed must be an instance of Carbon\\Carbon or DateTime');
44
        }
45
        return false;
46
    }
47
48 View Code Duplication
    public static function isArray($value, $strict = true)
0 ignored issues
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...
49
    {
50
        if (is_array($value) && count($value) > 0) {
51
            return true;
52
        }
53
54
        if ($strict) {
55
            throw new InvalidArgumentException('The value passed must be an array');
56
        }
57
        return false;
58
    }
59
60 View Code Duplication
    public static function isIterable($value, $strict = true)
0 ignored issues
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...
61
    {
62
        if ((is_array($value) || $value instanceof Traversable) && count($value) > 0) {
63
            return true;
64
        }
65
66
        if ($strict) {
67
            throw new InvalidArgumentException('The value passed must be iterable');
68
        }
69
        return false;
70
    }
71
72
    public static function isNotification($notification, $strict = true)
73
    {
74
        if (!is_a($notification, notifynder_config()->getNotificationModel())) {
75
            if ($strict) {
76
                throw new InvalidArgumentException('The value passed must be an Notification Model instance');
77
            }
78
            return false;
79
        }
80
        return true;
81
    }
82
}
83