Passed
Push — master ( cc6e69...e2c855 )
by Jakub
02:11
created

Assert   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 227
Duplicated Lines 36.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 82
loc 227
rs 7.4757
c 0
b 0
f 0
wmc 53
lcom 1
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A tryAssertion() 0 10 4
A same() 0 10 3
A notSame() 10 10 3
A true() 10 10 3
A false() 10 10 3
A null() 10 10 3
A notNull() 10 10 3
B contains() 5 19 8
B notContains() 7 19 8
A count() 10 10 4
A notCount() 10 10 4
B type() 0 17 7

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Assert, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester;
5
6
/**
7
 * Assertions
8
 *
9
 * @author Jakub Konečný
10
 * @copyright (c) 2015-2017, Jakub Konečný
11
 * @license https://spdx.org/licenses/BSD-3-Clause.html BSD-3-Clause
12
 */
13
abstract class Assert {
14
  use \Nette\StaticClass;
15
  
16
  /**
17
   * Tries an assertion
18
   * 
19
   * @param mixed $code Assertion to try
20
   * @param string $successText Text to print on success
21
   * @param string $failureText Text to print on failure
22
   */
23
  public static function tryAssertion($code, string $successText = "", string $failureText = ""): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
Unused Code introduced by
The method parameter $successText is never used
Loading history...
24
    $success = ($code == true);
25
    if(Environment::getShouldFail()) {
26
      $success = !$success;
27
    }
28
    if(!$success) {
29
      $message = ($failureText === "") ? "Assertion \"$code\" is not true." : $failureText;
30
    }
31
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
32
  }
33
  
34
  /**
35
   * Are both values same?
36
   * 
37
   * @param mixed $expected
38
   * @param mixed $actual
39
   */
40
  public static function same($expected, $actual): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
41
    $success = ($expected == $actual);
42
    if(Environment::getShouldFail()) {
43
      $success = !$success;
44
    }
45
    if(!$success) {
46
      $message = "The value is not $expected but $actual.";
47
    }
48
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
49
  }
50
  
51
  /**
52
   * Are not both values same?
53
   * 
54
   * @param mixed $expected
55
   * @param mixed $actual
56
   */
57 View Code Duplication
  public static function notSame($expected, $actual): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
58
    $success = ($expected !== $actual);
59
    if(Environment::getShouldFail()) {
60
      $success = !$success;
61
    }
62
    if(!$success) {
63
      $message = "The value is $expected.";
64
    }
65
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
66
  }
67
  
68
  /**
69
   * Is the expression true?
70
   * 
71
   * @param mixed $actual
72
   */
73 View Code Duplication
  public static function true($actual): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
74
    $success = ($actual == true);
75
    if(Environment::getShouldFail()) {
76
      $success = !$success;
77
    }
78
    if(!$success) {
79
      $message = "The expression is not true.";
80
    }
81
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
82
  }
83
  
84
  /**
85
   * Is the expression false?
86
   * 
87
   * @param mixed $actual
88
   */
89 View Code Duplication
  public static function false($actual): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
90
    $success = ($actual == false);
91
    if(Environment::getShouldFail()) {
92
      $success = !$success;
93
    }
94
    if(!$success) {
95
      $message = "The expression is not false.";
96
    }
97
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
98
  }
99
  
100
  /**
101
   * Is the value null?
102
   * 
103
   * @param mixed $actual
104
   */
105 View Code Duplication
  public static function null($actual): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
106
    $success = ($actual == NULL);
107
    if(Environment::getShouldFail()) {
108
      $success = !$success;
109
    }
110
    if(!$success) {
111
      $message = "The value is not null.";
112
    }
113
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
114
  }
115
  
116
  /**
117
   * Is not the value null?
118
   * 
119
   * @param mixed $actual
120
   */
121 View Code Duplication
  public static function notNull($actual): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
122
    $success = ($actual !== NULL);
123
    if(Environment::getShouldFail()) {
124
      $success = !$success;
125
    }
126
    if(!$success) {
127
      $message = "The value is null.";
128
    }
129
    Environment::testResult($message ?? "", $success);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
130
  }
131
  
132
  /**
133
   * Does $actual contain $needle?
134
   * 
135
   * @param string|array $needle
136
   * @param string|array $actual
137
   */
138
  public static function contains($needle, $actual): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
139
    if(!is_string($needle) AND !is_array($needle)) {
140
      Environment::testResult("The variable is not string or array.", false);
141
    } elseif(is_string($actual)) {
142
      if($needle !== "" AND strpos($actual, $needle) !== FALSE) {
143
        Environment::testResult("");
144
      } else {
145
        Environment::testResult("$needle is not in the variable.", false);
146
      }
147
    } elseif(is_array($actual)) {
148 View Code Duplication
      if(in_array($needle, $actual)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
149
        Environment::testResult("$needle is in the variable.");
150
      } else {
151
        Environment::testResult("$needle is not in the variable.", false);
152
      }
153
    } else {
154
      Environment::testResult("$needle is not in the variable.", false);
155
    }
156
  }
157
  
158
  /**
159
   * Does $actual not contain $needle?
160
   * 
161
   * @param string|array $needle
162
   * @param string|array $actual
163
   */
164
  public static function notContains($needle, $actual): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
165
    if(!is_string($needle) AND !is_array($needle)) {
166
      Environment::testResult("The variable is not string or array.", false);
167 View Code Duplication
    } elseif(is_string($actual)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
168
      if($needle === "" OR strpos($actual, $needle) === FALSE) {
169
        Environment::testResult("");
170
      } else {
171
        Environment::testResult("$needle is in the variable.", false);
172
      }
173
    } elseif(is_array($actual)) {
174
      if(!in_array($needle, $actual)) {
175
        Environment::testResult("");
176
      } else {
177
        Environment::testResult("$needle is in the variable.", false);
178
      }
179
    } else {
180
      Environment::testResult("$needle is not in the variable.", false);
181
    }
182
  }
183
  
184
  /**
185
   * Does $value contain $count items?
186
   *
187
   * @param string|array|\Countable $value
188
   */
189 View Code Duplication
  public static function count(int $count, $value): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
190
    if(!is_array($value) AND !$value instanceof \Countable) {
191
      Environment::testResult("The variable is not array or countable object.", false);
192
    } elseif(count($value) == $count) {
193
      Environment::testResult("");
194
    } else {
195
      $actual = count($value);
196
      Environment::testResult("Count of the variable is $actual.", false);
197
    }
198
  }
199
  
200
  /**
201
   * Does $value not contain $count items?
202
   *
203
   * @param string|array|\Countable $value
204
   */
205 View Code Duplication
  public static function notCount(int $count, $value): void {
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...
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
206
    if(!is_array($value) AND !$value instanceof \Countable) {
207
      Environment::testResult("The variable is not array or countable object.", false);
208
    } elseif(count($value) == $count) {
209
      $actual = count($value);
210
      Environment::testResult("Count of the variable is $actual.", false);
211
    } else {
212
      Environment::testResult("");
213
    }
214
  }
215
  
216
  /**
217
   * Is $value of type $type?
218
   * 
219
   * @param string|object $type
220
   * @param mixed $value
221
   */
222
  public static function type($type, $value): void {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after closing parenthesis; found 0
Loading history...
223
    if(!is_object($type) AND !is_string($type)) {
224
      Environment::testResult("Type must be string or object.", false);
225
    } elseif(in_array($type, ["array", "bool", "callable", "float",
226
      "int", "integer", "null", "object", "resource", "scalar", "string"], true)) {
227
      if(!call_user_func("is_$type", $value)) {
228
        Environment::testResult("The variable is " . gettype($value) . ".", false);
229
      } else {
230
        Environment::testResult("");
231
      }
232
    } elseif(!$value instanceof $type) {
233
      $actual = is_object($value) ? get_class($value) : gettype($value);
234
      Environment::testResult("The variable is instance of $actual.", false);
235
    } else {
236
      Environment::testResult("");
237
    }
238
  }
239
}
240
?>