Offset::logic()   C
last analyzed

Complexity

Conditions 21
Paths 88

Size

Total Lines 59
Code Lines 29

Duplication

Lines 6
Ratio 10.17 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 59
rs 6.2662
cc 21
eloc 29
nc 88
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the SomeWork/OffsetPage/Logic package.
5
 *
6
 * (c) Pinchuk Igor <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace SomeWork\OffsetPage\Logic;
13
14
class Offset
15
{
16
    /**
17
     * @param int $offset
18
     * @param int $limit
19
     * @param int $nowCount
20
     *
21
     * @throws \LogicException
22
     * @throws \SomeWork\OffsetPage\Logic\AlreadyGetNeededCountException
23
     *
24
     * @return OffsetLogicResult
25
     */
26
    public static function logic($offset, $limit, $nowCount = 0)
27
    {
28
        $offset = (int) $offset;
29
        $limit = (int) $limit;
30
        $nowCount = (int) $nowCount;
31
32
        $offset = $offset >= 0 ? $offset : 0;
33
        $limit = $limit >= 0 ? $limit : 0;
34
        $nowCount = $nowCount >= 0 ? $nowCount : 0;
35
36
        /*
37
         * Means that you should get all
38
         */
39 View Code Duplication
        if ($offset === 0 && $limit === 0 && $nowCount === 0) {
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...
40
            return new OffsetLogicResult(0, 0);
41
        }
42
43 View Code Duplication
        if ($offset === 0 && $limit > 0 && $nowCount === 0) {
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...
44
            return new OffsetLogicResult(1, $limit);
45
        }
46
47
        if ($offset > 0 && $limit === 0) {
48
            return new OffsetLogicResult(2, $nowCount + $offset);
49
        }
50
51
        if ($nowCount > 0) {
52
            if ($limit > $nowCount) {
53
                return static::logic($offset + $nowCount, $limit - $nowCount);
54
            }
55
            /*
56
             * Means that you should stop fetching
57
             */
58
            throw new AlreadyGetNeededCountException('Limit <= no count. You should stop asking (:');
59
        }
60
61
        if ($offset > 0 && $limit > 0) {
62
            if ($offset === $limit) {
63
                return new OffsetLogicResult(2, $limit);
64
            }
65
            if ($offset < $limit) {
66
                return new OffsetLogicResult(2, $offset);
67
            }
68
            if ($offset > $limit) {
69
                for ($i = $limit; $i > 0; $i--) {
70
                    if ($offset % $i === 0) {
71
                        return new OffsetLogicResult(
72
                            ($offset / $i) + 1,
73
                            $i
74
                        );
75
                    }
76
                }
77
            }
78
        }
79
80
        /*
81
         * Really its can be here oO
82
         */
83
        throw new \LogicException('Something gone wrong');
84
    }
85
}
86