Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

StringToBool::execute()   B

Complexity

Conditions 11
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 7.1162
c 0
b 0
f 0
cc 11
eloc 14
nc 5
nop 2
crap 11

How to fix   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 namespace Limoncello\Validation\Rules\Converters;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
20
use Limoncello\Validation\Contracts\Execution\ContextInterface;
21
use Limoncello\Validation\Rules\ExecuteRule;
22
23
/**
24
 * @package Limoncello\Validation
25
 */
26
final class StringToBool extends ExecuteRule
27
{
28
    /**
29
     * @param mixed            $value
30
     * @param ContextInterface $context
31
     *
32
     * @return array
33
     *
34
     * @SuppressWarnings(PHPMD.StaticAccess)
35
     * @SuppressWarnings(PHPMD.ElseExpression)
36
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
37
     */
38 2
    public static function execute($value, ContextInterface $context): array
39
    {
40 2
        if (is_string($value) === true) {
41 2
            $lcValue = strtolower($value);
42 2
            if ($lcValue === 'true' || $lcValue === '1' || $lcValue === 'on' || $lcValue === 'yes') {
43 2
                $reply = static::createSuccessReply(true);
44 2
            } elseif ($lcValue === 'false' || $lcValue === '0' || $lcValue === 'off' || $lcValue === 'no') {
45 1
                $reply = static::createSuccessReply(false);
46
            } else {
47 2
                $reply = static::createErrorReply($context, $value, ErrorCodes::IS_BOOL);
48
            }
49 1
        } elseif (is_bool($value) === true) {
50 1
            $reply = static::createSuccessReply($value);
51
        } else {
52 1
            $reply = static::createErrorReply($context, $value, ErrorCodes::IS_BOOL);
53
        }
54
55 2
        return $reply;
56
    }
57
}
58