Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f17968

Browse files
committed
Refactor Subscriber package using traits
1 parent 6f55ccd commit 9f17968

13 files changed

Lines changed: 188 additions & 204 deletions

src/Subscriber/ConsoleDots.php

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
namespace Psecio\Parse\Subscriber;
44

55
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6-
use Symfony\Component\Console\Output\OutputInterface;
7-
use Psecio\Parse\Event\Events;
86
use Psecio\Parse\Event\FileEvent;
97
use Psecio\Parse\Event\IssueEvent;
108
use Psecio\Parse\Event\MessageEvent;
119

1210
/**
1311
* Display phpunit style dots to visualize scan progression
1412
*/
15-
class ConsoleDots implements EventSubscriberInterface, Events
13+
class ConsoleDots implements EventSubscriberInterface
1614
{
17-
/**
18-
* @var OutputInterface Registered output
19-
*/
20-
private $output;
15+
use Helper\SubscriberTrait, Helper\OutputTrait;
2116

2217
/**
2318
* @var string One charactes status descriptor
@@ -34,16 +29,6 @@ class ConsoleDots implements EventSubscriberInterface, Events
3429
*/
3530
private $fileCount;
3631

37-
/**
38-
* Register output interface
39-
*
40-
* @param OutputInterface $output
41-
*/
42-
public function __construct(OutputInterface $output)
43-
{
44-
$this->output = $output;
45-
}
46-
4732
/**
4833
* Set number of status chars per line
4934
*
@@ -55,24 +40,6 @@ public function setLineLength($lineLength)
5540
$this->lineLength = $lineLength;
5641
}
5742

58-
/**
59-
* Returns an array of event names this subscriber wants to listen to
60-
*
61-
* @return array The event names to listen to
62-
*/
63-
public static function getSubscribedEvents()
64-
{
65-
return [
66-
self::SCAN_START => 'onScanStart',
67-
self::SCAN_COMPLETE => 'onScanComplete',
68-
self::FILE_OPEN => 'onFileOpen',
69-
self::FILE_CLOSE => 'onFileClose',
70-
self::FILE_ISSUE => 'onFileIssue',
71-
self::FILE_ERROR => 'onFileError',
72-
self::DEBUG => 'onDebug'
73-
];
74-
}
75-
7643
/**
7744
* Write header on scan start
7845
*
@@ -83,15 +50,6 @@ public function onScanStart()
8350
$this->fileCount = 0;
8451
}
8552

86-
/**
87-
* Ignore scan complete
88-
*
89-
* @return null
90-
*/
91-
public function onScanComplete()
92-
{
93-
}
94-
9553
/**
9654
* Set status to valid on file open
9755
*
@@ -138,26 +96,4 @@ public function onFileError(MessageEvent $event)
13896
{
13997
$this->status = '<error>E</error>';
14098
}
141-
142-
/**
143-
* Ignore debug events
144-
*
145-
* @param MessageEvent $event
146-
* @return null
147-
*/
148-
public function onDebug(MessageEvent $event)
149-
{
150-
}
151-
152-
/**
153-
* Write to console
154-
*
155-
* @param string $format sprintf format string
156-
* @param mixed ...$arg Any number of sprintf arguments
157-
* @return null
158-
*/
159-
protected function write()
160-
{
161-
$this->output->write(call_user_func_array('sprintf', func_get_args()));
162-
}
16399
}

src/Subscriber/ConsoleLines.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace Psecio\Parse\Subscriber;
44

5+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
56
use Psecio\Parse\Event\FileEvent;
67
use Psecio\Parse\Event\IssueEvent;
78
use Psecio\Parse\Event\MessageEvent;
89

910
/**
1011
* Display descriptive lines to visualize scan progression
1112
*/
12-
class ConsoleLines extends ConsoleDots
13+
class ConsoleLines implements EventSubscriberInterface
1314
{
15+
use Helper\SubscriberTrait, Helper\OutputTrait;
16+
1417
/**
1518
* Write path on file open
1619
*
@@ -22,15 +25,6 @@ public function onFileOpen(FileEvent $event)
2225
$this->write("[PARSE] %s\n", $event->getFile()->getPath());
2326
}
2427

25-
/**
26-
* Ignore file close
27-
*
28-
* @return null
29-
*/
30-
public function onFileClose()
31-
{
32-
}
33-
3428
/**
3529
* Write issue as one line
3630
*

src/Subscriber/ConsoleProgressBar.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace Psecio\Parse\Subscriber;
44

5-
use Psecio\Parse\Event\Events;
65
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
76
use Symfony\Component\Console\Helper\ProgressBar;
87

98
/**
109
* Display a progress bar to visualize scan progression
1110
*/
12-
class ConsoleProgressBar implements EventSubscriberInterface, Events
11+
class ConsoleProgressBar implements EventSubscriberInterface
1312
{
13+
use Helper\SubscriberTrait;
14+
1415
/**
1516
* The progress bar format used of the number if steps is known
1617
*/
@@ -39,20 +40,6 @@ public function __construct(ProgressBar $progressBar)
3940
);
4041
}
4142

42-
/**
43-
* Returns an array of event names this subscriber wants to listen to
44-
*
45-
* @return array The event names to listen to
46-
*/
47-
public static function getSubscribedEvents()
48-
{
49-
return [
50-
self::SCAN_START => 'onScanStart',
51-
self::SCAN_COMPLETE => 'onScanComplete',
52-
self::FILE_CLOSE => 'onFileClose',
53-
];
54-
}
55-
5643
/**
5744
* Reset progress bar on scan start
5845
*

src/Subscriber/ConsoleReport.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@
33
namespace Psecio\Parse\Subscriber;
44

55
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6-
use Symfony\Component\Console\Output\OutputInterface;
7-
use Psecio\Parse\Event\Events;
86
use Psecio\Parse\Event\IssueEvent;
97
use Psecio\Parse\Event\MessageEvent;
108

119
/**
12-
* Print report to output at scan complete
10+
* Print report at scan complete
1311
*/
14-
class ConsoleReport implements EventSubscriberInterface, Events
12+
class ConsoleReport implements EventSubscriberInterface
1513
{
16-
/**
17-
* @var OutputInterface Registered output
18-
*/
19-
private $output;
14+
use Helper\SubscriberTrait, Helper\OutputTrait;
2015

2116
/**
2217
* @var integer Number of scanned files
@@ -33,32 +28,6 @@ class ConsoleReport implements EventSubscriberInterface, Events
3328
*/
3429
private $errors;
3530

36-
/**
37-
* Register output interface
38-
*
39-
* @param OutputInterface $output
40-
*/
41-
public function __construct(OutputInterface $output)
42-
{
43-
$this->output = $output;
44-
}
45-
46-
/**
47-
* Returns an array of event names this subscriber wants to listen to
48-
*
49-
* @return array The event names to listen to
50-
*/
51-
public static function getSubscribedEvents()
52-
{
53-
return [
54-
self::SCAN_START => 'onScanStart',
55-
self::SCAN_COMPLETE => 'onScanComplete',
56-
self::FILE_OPEN => 'onFileOpen',
57-
self::FILE_ISSUE => 'onFileIssue',
58-
self::FILE_ERROR => 'onFileError'
59-
];
60-
}
61-
6231
/**
6332
* Reset values on scan start
6433
*

src/Subscriber/ExitCodeCatcher.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,19 @@
33
namespace Psecio\Parse\Subscriber;
44

55
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6-
use Psecio\Parse\Event\Events;
76

87
/**
98
* Capture the exit status code of a scan
109
*/
11-
class ExitCodeCatcher implements EventSubscriberInterface, Events
10+
class ExitCodeCatcher implements EventSubscriberInterface
1211
{
12+
use Helper\SubscriberTrait;
13+
1314
/**
1415
* @var integer Suggested exit code
1516
*/
1617
private $exitCode = 0;
1718

18-
/**
19-
* Returns an array of event names this subscriber wants to listen to
20-
*
21-
* @return array The event names to listen to
22-
*/
23-
public static function getSubscribedEvents()
24-
{
25-
return [
26-
self::FILE_ISSUE => 'onFileIssue',
27-
self::FILE_ERROR => 'onFileError'
28-
];
29-
}
30-
3119
/**
3220
* Get suggested exit code
3321
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Psecio\Parse\Subscriber\Helper;
4+
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
/**
8+
* Helper to simplify writing to the console
9+
*/
10+
trait OutputTrait
11+
{
12+
/**
13+
* @var OutputInterface Registered output
14+
*/
15+
protected $output;
16+
17+
/**
18+
* Register output interface
19+
*
20+
* @param OutputInterface $output
21+
*/
22+
public function __construct(OutputInterface $output)
23+
{
24+
$this->output = $output;
25+
}
26+
27+
/**
28+
* Write to console
29+
*
30+
* @param string $format sprintf format string
31+
* @param mixed ...$arg Any number of sprintf arguments
32+
* @return null
33+
*/
34+
protected function write()
35+
{
36+
$this->output->write(call_user_func_array('sprintf', func_get_args()));
37+
}
38+
}

0 commit comments

Comments
 (0)