Offset   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
ccs 14
cts 15
cp 0.9333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toString() 0 12 2
A convertToInteger() 0 14 3
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