Limit::convertToInteger()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.0416
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Puzzle\QueryBuilder\Queries\Snippets;
6
7
use Puzzle\QueryBuilder\Snippet;
8
use Puzzle\Pieces\StringManipulation;
9
10
class Limit implements Snippet
11
{
12
    use StringManipulation;
13
14
    private
15
        $limit;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $limit.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
17
    /**
18
     * @param int|string $limit
19
     */
20 13
    public function __construct($limit)
21
    {
22 13
        $this->limit = $this->convertToInteger($limit);
23 13
    }
24
25 13
    public function toString(): string
26
    {
27 13
        if(is_null($this->limit))
28
        {
29 5
            return '';
30
        }
31
32 8
        return sprintf(
33 8
            'LIMIT %d',
34 8
            $this->limit
35
        );
36
    }
37
38 13
    private function convertToInteger($value): ?int
39
    {
40 13
        if($this->isConvertibleToString($value) === false)
41
        {
42
            throw new \InvalidArgumentException("Limit argument must be an integer or an integer wrapped into a string");
43
        }
44
45 13
        if(preg_match('~^[\d]+$~', (string) $value))
46
        {
47 8
            return (int) $value;
48
        }
49
50 5
        return null;
51
    }
52
}
53