Offset::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 Offset implements Snippet
11
{
12
    use StringManipulation;
13
14
    private
15
        $offset;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $offset.

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 $offset
19
     */
20 11
    public function __construct($offset)
21
    {
22 11
        $this->offset = $this->convertToInteger($offset);
23 11
    }
24
25 10
    public function toString(): string
26
    {
27 10
        if(is_null($this->offset))
28
        {
29 4
            return '';
30
        }
31
32 6
        return sprintf(
33 6
            'OFFSET %d',
34 6
            $this->offset
35
        );
36
    }
37
38 11
    private function convertToInteger($value): ?int
39
    {
40 11
        if($this->isConvertibleToString($value) === false)
41
        {
42
            throw new \InvalidArgumentException("Offset argument must be an integer or an integer wrapped into a string");
43
        }
44
45 11
        if(preg_match('~^[\d]+$~', (string) $value))
46
        {
47 7
            return (int) $value;
48
        }
49
50 4
        return null;
51
    }
52
}
53