1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Solidifier\EventSubscribers; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
6
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Solidifier\Events\TraverseEnd; |
9
|
|
|
use Solidifier\Events\ChangeFile; |
10
|
|
|
use Solidifier\Defect; |
11
|
|
|
|
12
|
|
|
class Console implements EventSubscriberInterface |
13
|
|
|
{ |
14
|
|
|
private |
15
|
|
|
$counter, |
|
|
|
|
16
|
|
|
$currentFile, |
17
|
|
|
$output; |
18
|
|
|
|
19
|
|
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
return array( |
22
|
|
|
Defect::EVENT_NAME => array('onDefect'), |
23
|
|
|
TraverseEnd::EVENT_NAME => array('postMortemReport'), |
24
|
|
|
ChangeFile::EVENT_NAME => array('setCurrentFile'), |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->output = new NullOutput(); |
|
|
|
|
31
|
|
|
$this->currentFile = null; |
32
|
|
|
$this->counter = 0; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setOutput(OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$this->output = $output; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setCurrentFile(ChangeFile $event) |
43
|
|
|
{ |
44
|
|
|
$this->currentFile = $event->getCurrentFile(); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function onDefect(Defect $event) |
50
|
|
|
{ |
51
|
|
|
$this->output->writeln(sprintf( |
52
|
|
|
"<fg=white;options=bold>%s @ l%d</fg=white;options=bold> : %s", |
53
|
|
|
$this->currentFile, |
54
|
|
|
$event->getLine(), |
55
|
|
|
$this->formatMessage($event->getMessage()) |
56
|
|
|
)); |
57
|
|
|
|
58
|
|
|
$this->counter++; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function formatMessage($message) |
62
|
|
|
{ |
63
|
|
|
return strtr($message, array( |
64
|
|
|
'id>' => 'comment>', |
65
|
|
|
'type>' => 'info>', |
66
|
|
|
'method>' => 'info>', |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function postMortemReport(TraverseEnd $event) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$this->output->writeln(sprintf( |
73
|
|
|
'<comment>%d defect%s found</comment>', |
74
|
|
|
$this->counter, |
75
|
|
|
$this->counter > 0 ? 's' : '' |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.