PlayTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A play() 0 10 2
A getClassName() 0 6 1
1
<?php
2
3
namespace MiniGame\Entity;
4
5
use MiniGame\Exceptions\IllegalMoveException;
6
use MiniGame\GameResult;
7
use MiniGame\Move;
8
9
trait PlayTrait
10
{
11
    /**
12
     * Allows the player to play the game
13
     *
14
     * @param  PlayerId $playerId
15
     * @param  Move $move
16
     *
17
     * @throws IllegalMoveException
18
     *
19
     * @return GameResult
20
     */
21 6
    public function play(PlayerId $playerId, Move $move)
22
    {
23 6
        $playMethod = 'play' . $this->getClassName($move);
24
25 6
        if (! method_exists($this, $playMethod)) {
26 3
            throw new IllegalMoveException($move, 'Error: move was not recognized!');
27
        }
28
29 3
        return $this->$playMethod($playerId, $move);
30
    }
31
32
    /**
33
     * @param  Move $move
34
     *
35
     * @return string
36
     */
37 6
    private function getClassName(Move $move)
38
    {
39 6
        $namespaceArray = explode('\\', get_class($move));
40 6
        $underscoreArray = explode('_', end($namespaceArray));
41 6
        return end($underscoreArray);
42
    }
43
}
44