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

CaseMapper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C map() 0 37 12
A fromSnakeCase() 0 4 1
A fromKebabCase() 0 4 1
A fromPascalCase() 0 4 1
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