HandleClassNameWithoutSuffixInflector   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 37
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A inflect() 0 10 2
1
<?php
2
namespace League\Tactician\Handler\MethodNameInflector;
3
4
/**
5
 * Returns a method name that is handle + the last portion of the class name
6
 * but also without a given suffix, typically "Command". This allows you to
7
 * handle multiple commands on a single object but with slightly less annoying
8
 * method names.
9
 *
10
 * The string removal is case sensitive.
11
 *
12
 * Examples:
13
 *  - \CompleteTaskCommand     => $handler->handleCompleteTask()
14
 *  - \My\App\DoThingCommand   => $handler->handleDoThing()
15
 */
16
class HandleClassNameWithoutSuffixInflector extends HandleClassNameInflector
17
{
18
    /**
19
     * @var string
20
     */
21
    private $suffix;
22
23
    /**
24
     * @var int
25
     */
26
    private $suffixLength;
27
28
    /**
29
     * @param string $suffix The string to remove from end of each class name
30
     */
31 3
    public function __construct($suffix = 'Command')
32
    {
33 3
        $this->suffix = $suffix;
34 3
        $this->suffixLength = strlen($suffix);
35 3
    }
36
37
    /**
38
     * @param object $command
39
     * @param object $commandHandler
40
     * @return string
41
     */
42 3
    public function inflect($command, $commandHandler)
43
    {
44 3
        $methodName = parent::inflect($command, $commandHandler);
45
46 3
        if (substr($methodName, $this->suffixLength * -1) !== $this->suffix) {
47 1
            return $methodName;
48
        }
49
50 2
        return substr($methodName, 0, strlen($methodName) - $this->suffixLength);
51
    }
52
}
53