HandleClassNameWithoutSuffixInflector::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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