Completed
Push — master ( ef244f...c2eef9 )
by CodexShaper
06:24
created

Type::regex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace CodexShaper\DBM\Database\Drivers\MongoDB;
4
5
use MongoDB\BSON\Binary;
6
use MongoDB\BSON\Decimal128 as Decimal;
7
use MongoDB\BSON\Javascript;
8
use MongoDB\BSON\MaxKey;
9
use MongoDB\BSON\MinKey;
10
use MongoDB\BSON\ObjectId;
11
use MongoDB\BSON\Regex;
12
use MongoDB\BSON\Timestamp;
13
use MongoDB\BSON\UTCDateTime;
14
15
class Type
16
{
17
    protected static $types = [
18
        'binary',
19
        'decimal',
20
        'javascript',
21
        'maxKey',
22
        'minKey',
23
        'objectId',
24
        'regex',
25
        'timestamp',
26
        'dateTime',
27
        'string',
28
        'arrayType',
29
        'object',
30
        'boolean',
31
        'double',
32
        'null',
33
        'integer',
34
        'longInteger',
35
        'relationship',
36
    ];
37
    public static function binary(string $value, int $type = Binary::TYPE_GENERIC)
38
    {
39
        return new Binary($value, $type);
40
    }
41
42
    public static function decimal(string $value)
43
    {
44
        return new Decimal($value);
45
    }
46
47
    public static function javascript(string $code, $scope = [])
48
    {
49
        if (!is_array($scope) || !is_array()) {
0 ignored issues
show
Bug introduced by
The call to is_array() has too few arguments starting with var. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        if (!is_array($scope) || !/** @scrutinizer ignore-call */ is_array()) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50
            throw new \Exception($scope . " should be array or object");
51
52
        }
53
54
        return new Javascript($code, $scope);
55
    }
56
57
    public static function maxKey()
58
    {
59
        return new MaxKey();
60
    }
61
62
    public static function minKey()
63
    {
64
        return new MinKey();
65
    }
66
67
    public static function objectId(string $value)
68
    {
69
        return new ObjectId($value);
70
    }
71
72
    public static function regex(string $pattern, string $flags)
0 ignored issues
show
Unused Code introduced by
The parameter $flags is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
    public static function regex(string $pattern, /** @scrutinizer ignore-unused */ string $flags)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pattern is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

72
    public static function regex(/** @scrutinizer ignore-unused */ string $pattern, string $flags)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        return new Regex($value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
75
    }
76
77
    public static function timestamp(int $increment, int $timestamp)
78
    {
79
        return new Timestamp($increment, $timestamp);
80
    }
81
82
    public static function dateTime($milliseconds = null)
83
    {
84
        if (!is_int($milliseconds) || !is_float($milliseconds) || !is_string($milliseconds) || !$milliseconds instanceof \DateTimeInterface) {
0 ignored issues
show
introduced by
The condition is_float($milliseconds) is always false.
Loading history...
85
            throw new \Exception($milliseconds . " integer or float or string or instance of DateTimeInterface");
86
87
        }
88
        return new UTCDateTime($milliseconds);
89
    }
90
91
    /*
92
     * Custom Types
93
     */
94
95
    public static function string($value)
96
    {
97
        return (string) $value;
98
    }
99
100
    public static function arrayType($value)
101
    {
102
        return (array) $value;
103
    }
104
105
    public static function object($value)
106
    {
107
        return (object) $value;
108
    }
109
110
    public static function boolean($value)
111
    {
112
        return (boolean) $value;
113
    }
114
115
    public static function double($value)
116
    {
117
        return (double) $value;
118
    }
119
120
    public static function null()
121
    {
122
        return null;
123
    }
124
125
    public static function integer($value)
126
    {
127
        return (int) $value;
128
    }
129
130
    public static function longInteger($value)
131
    {
132
        return (int) $value;
133
    }
134
135
    public static function relationship($value)
136
    {
137
        return $value;
138
    }
139
140
    public static function getTypes()
141
    {
142
        return (array) self::$types;
143
    }
144
}
145