Completed
Push — master ( 1e376d...b0060a )
by Changwan
05:50
created

CaseMapper::map()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 37
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 30
nc 24
nop 1
dl 0
loc 37
ccs 0
cts 34
cp 0
crap 156
rs 5.1612
c 0
b 0
f 0

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
2
namespace Wandu\Support\Mapper;
3
4
use InvalidArgumentException;
5
use Wandu\Support\Contracts\MapperInterface;
6
7
class CaseMapper implements MapperInterface
8
{
9
    const SNAKECASE = 1; // snake_case
10
    const PASCALCASE = 2; // PascalCase
11
    const CAMELCASE = 3; // camelCase
12
    const KEBABCASE = 4; // kebab-case 
13
    
14
    /** @var int */
15
    private $from;
16
17
    /** @var int */
18
    private $to;
19
20
    /**
21
     * @param int $from
22
     * @param int $to
23
     */
24
    public function __construct($from, $to)
25
    {
26
        $this->from = $from;
27
        $this->to = $to;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function map($name)
34
    {
35
        // 4 cases, [SNAKECASE, SNAKECASE], [PASCALCASE, PASCALCASE], [CAMELCASE, CAMELCASE], [KEBABCASE, KEBABCASE]
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
        if ($this->from === $this->to) return $name;
37
38
        
39
        switch ($this->from) {
40
            case static::PASCALCASE:
41
                if ($this->to === static::CAMELCASE) return lcfirst($name);
42
                $tokens = $this->fromPascalCase($name);
43
                break;
44
            case static::CAMELCASE:
45
                if ($this->to === static::PASCALCASE) return ucfirst($name);
46
                $tokens = $this->fromPascalCase($name);
47
                break;
48
            case static::KEBABCASE:
49
                $tokens = $this->fromKebabCase($name);
50
                break;
51
            case static::SNAKECASE:
52
                $tokens = $this->fromSnakeCase($name);
53
                break;
54
            default:
55
                throw new InvalidArgumentException("Argument 1 passed to " . __METHOD__ . "() must be in [1, 2, 3, 4]");
56
        }
57
        switch ($this->to) {
58
            case static::PASCALCASE:
59
                return str_replace(' ', '', ucwords($tokens));
60
            case static::CAMELCASE:
61
                return lcfirst(str_replace(' ', '', ucwords($tokens)));
62
            case static::KEBABCASE:
63
                return str_replace(' ', '-', strtolower($tokens));
64
            case static::SNAKECASE:
65
                return str_replace(' ', '_', strtolower($tokens));
66
            default:
67
                throw new InvalidArgumentException("Argument 1 passed to " . __METHOD__ . "() must be in [1, 2, 3, 4]");
68
        }
69
    }
70
71
    private function fromSnakeCase($name)
72
    {
73
        return str_replace('_', ' ', $name);
74
    }
75
76
    private function fromKebabCase($name)
77
    {
78
        return str_replace('-', ' ', $name);
79
    }
80
81
    private function fromPascalCase($name)
82
    {
83
        return preg_replace('/((?<=[^$])[A-Z0-9])/', ' $1', $name);
84
    }
85
}
86