Passed
Branch benchmark (b81757)
by Jose
10:16
created

InputValidator::isPositiveInteger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style introduced by
This file is missing a doc comment.
Loading history...
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
Coding Style introduced by
Filename "InputValidator.php" doesn't match the expected filename "inputvalidator.php"
Loading history...
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace JMGQ\AStar\Benchmark;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Style\StyleInterface;
7
8
class InputValidator
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
9
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class InputValidator
Loading history...
10
    private $output;
0 ignored issues
show
Coding Style introduced by
Private member variable "output" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
Coding Style introduced by
Private member variable "output" must be prefixed with an underscore
Loading history...
11
12
    public function __construct(StyleInterface $output)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
13
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
14
        $this->output = $output;
15
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
16
17
    public function validate(InputInterface $input)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
18
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
19
        $hasValidInput = true;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of true please use TRUE.
Loading history...
20
21
        $sizes = $input->getOption(BenchmarkCommand::SIZE_OPTION);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
22
        $iterations = $input->getOption(BenchmarkCommand::ITERATIONS_OPTION);
23
        $seed = $input->getOption(BenchmarkCommand::SEED_OPTION);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
24
25
        foreach ($sizes as $size) {
0 ignored issues
show
Bug introduced by
The expression $sizes of type string|array<integer,string>|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
26
            if (!$this->isPositiveInteger($size)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
27
                $this->output->error('The size must be an integer greater than 0');
28
                $hasValidInput = false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
29
            }
30
        }
31
32
        if (!$this->isPositiveInteger($iterations)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
33
            $this->output->error('The number of iterations must be an integer greater than 0');
34
            $hasValidInput = false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
35
        }
36
37
        if (!$this->isOptionalInteger($seed)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
38
            $this->output->error('The seed must be an integer');
39
            $hasValidInput = false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
40
        }
41
42
        return $hasValidInput;
43
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end validate()
Loading history...
44
45
    private function isPositiveInteger($value)
0 ignored issues
show
Coding Style introduced by
Private method name "InputValidator::isPositiveInteger" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
46
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
47
        $positiveInteger = filter_var($value, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
48
49
        return $positiveInteger !== false;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
50
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isPositiveInteger()
Loading history...
51
52
    private function isOptionalInteger($value)
0 ignored issues
show
Coding Style introduced by
Private method name "InputValidator::isOptionalInteger" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
53
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
54
        $integer = filter_var($value, FILTER_VALIDATE_INT);
55
56
        return $integer !== false || $value === null;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of false please use FALSE.
Loading history...
Coding Style introduced by
Boolean operators are not allowed outside of control structure conditions
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL should be uppercase as per the configured coding-style; instead of null please use NULL.
Loading history...
57
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isOptionalInteger()
Loading history...
58
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
59