HandleClassNameWithoutSuffixInflector   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 42
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0

3 Methods

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