Skip to content

Commit 012ce3d

Browse files
committed
added PHP 7.1 typehints
1 parent e24ba12 commit 012ce3d

36 files changed

Lines changed: 263 additions & 465 deletions

src/CodeCoverage/Collector.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ class Collector
2020
private static $collector;
2121

2222

23-
/**
24-
* @return bool
25-
*/
26-
public static function isStarted()
23+
public static function isStarted(): bool
2724
{
2825
return self::$file !== null;
2926
}
3027

3128

3229
/**
3330
* Starts gathering the information for code coverage.
34-
* @param string
35-
* @return void
3631
* @throws \LogicException
3732
*/
38-
public static function start($file)
33+
public static function start(string $file): void
3934
{
4035
if (self::isStarted()) {
4136
throw new \LogicException('Code coverage collector has been already started.');
@@ -63,7 +58,7 @@ public static function start($file)
6358
/**
6459
* Flushes all gathered information. Effective only with PHPDBG collector.
6560
*/
66-
public static function flush()
61+
public static function flush(): void
6762
{
6863
if (self::isStarted() && self::$collector === 'collectPhpDbg') {
6964
self::save();
@@ -73,10 +68,9 @@ public static function flush()
7368

7469
/**
7570
* Saves information about code coverage. Can be called repeatedly to free memory.
76-
* @return void
7771
* @throws \LogicException
7872
*/
79-
public static function save()
73+
public static function save(): void
8074
{
8175
if (!self::isStarted()) {
8276
throw new \LogicException('Code coverage collector has not been started.');
@@ -99,9 +93,8 @@ public static function save()
9993

10094
/**
10195
* Collects information about code coverage.
102-
* @return array
10396
*/
104-
private static function collectXdebug()
97+
private static function collectXdebug(): array
10598
{
10699
$positive = $negative = [];
107100

@@ -125,9 +118,8 @@ private static function collectXdebug()
125118

126119
/**
127120
* Collects information about code coverage.
128-
* @return array
129121
*/
130-
private static function collectPhpDbg()
122+
private static function collectPhpDbg(): array
131123
{
132124
$positive = phpdbg_end_oplog();
133125
$negative = phpdbg_get_executable();

src/CodeCoverage/Generators/AbstractGenerator.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class AbstractGenerator
3838
* @param string path to coverage.dat file
3939
* @param string path to covered source file or directory
4040
*/
41-
public function __construct($file, $source = null)
41+
public function __construct(string $file, string $source = null)
4242
{
4343
if (!is_file($file)) {
4444
throw new \Exception("File '$file' is missing.");
@@ -69,14 +69,14 @@ public function __construct($file, $source = null)
6969
}
7070

7171

72-
public function render($file = null)
72+
public function render(string $file = null): void
7373
{
7474
$handle = $file ? @fopen($file, 'w') : STDOUT; // @ is escalated to exception
7575
if (!$handle) {
7676
throw new \Exception("Unable to write to file '$file'.");
7777
}
7878

79-
ob_start(function ($buffer) use ($handle) { fwrite($handle, $buffer); }, 4096);
79+
ob_start(function (string $buffer) use ($handle) { fwrite($handle, $buffer); }, 4096);
8080
try {
8181
$this->renderSelf();
8282
} catch (\Exception $e) {
@@ -93,25 +93,19 @@ public function render($file = null)
9393
}
9494

9595

96-
/**
97-
* @return float
98-
*/
99-
public function getCoveredPercent()
96+
public function getCoveredPercent(): float
10097
{
10198
return $this->totalSum ? $this->coveredSum * 100 / $this->totalSum : 0;
10299
}
103100

104101

105-
/**
106-
* @return \Iterator
107-
*/
108-
protected function getSourceIterator()
102+
protected function getSourceIterator(): \Iterator
109103
{
110104
$iterator = is_dir($this->source)
111105
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->source))
112106
: new \ArrayIterator([new \SplFileInfo($this->source)]);
113107

114-
return new \CallbackFilterIterator($iterator, function (\SplFileInfo $file) {
108+
return new \CallbackFilterIterator($iterator, function (\SplFileInfo $file): bool {
115109
return $file->getBasename()[0] !== '.' // . or .. or .gitignore
116110
&& in_array($file->getExtension(), $this->acceptFiles, true);
117111
});

src/CodeCoverage/Generators/CloverXMLGenerator.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CloverXMLGenerator extends AbstractGenerator
3131
];
3232

3333

34-
public function __construct($file, $source = null)
34+
public function __construct(string $file, string $source = null)
3535
{
3636
if (!extension_loaded('dom')) {
3737
throw new \LogicException('CloverXML generator requires DOM extension to be loaded.');
@@ -40,7 +40,7 @@ public function __construct($file, $source = null)
4040
}
4141

4242

43-
protected function renderSelf()
43+
protected function renderSelf(): void
4444
{
4545
$time = (string) time();
4646
$parser = new PhpParser;
@@ -147,10 +147,7 @@ protected function renderSelf()
147147
}
148148

149149

150-
/**
151-
* @return \stdClass
152-
*/
153-
private function calculateClassMetrics(\stdClass $info, array $coverageData = null)
150+
private function calculateClassMetrics(\stdClass $info, array $coverageData = null): \stdClass
154151
{
155152
$stats = (object) [
156153
'methodCount' => count($info->methods),
@@ -181,10 +178,7 @@ private function calculateClassMetrics(\stdClass $info, array $coverageData = nu
181178
}
182179

183180

184-
/**
185-
* @return array
186-
*/
187-
private static function analyzeMethod(\stdClass $info, array $coverageData = null)
181+
private static function analyzeMethod(\stdClass $info, array $coverageData = null): array
188182
{
189183
$count = 0;
190184
$coveredCount = 0;
@@ -206,15 +200,15 @@ private static function analyzeMethod(\stdClass $info, array $coverageData = nul
206200
}
207201

208202

209-
private static function appendMetrics(\stdClass $summary, \stdClass $add)
203+
private static function appendMetrics(\stdClass $summary, \stdClass $add): void
210204
{
211205
foreach ($add as $name => $value) {
212206
$summary->{$name} += $value;
213207
}
214208
}
215209

216210

217-
private static function setMetricAttributes(DOMElement $element, \stdClass $metrics)
211+
private static function setMetricAttributes(DOMElement $element, \stdClass $metrics): void
218212
{
219213
foreach ($metrics as $name => $value) {
220214
$element->setAttribute(self::$metricAttributesMap[$name], (string) $value);

src/CodeCoverage/Generators/HtmlGenerator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ class HtmlGenerator extends AbstractGenerator
3030
/**
3131
* @param string path to coverage.dat file
3232
* @param string path to source file/directory
33-
* @param string
3433
*/
35-
public function __construct($file, $source = null, $title = null)
34+
public function __construct(string $file, string $source = null, string $title = null)
3635
{
3736
parent::__construct($file, $source);
3837
$this->title = $title;
3938
}
4039

4140

42-
protected function renderSelf()
41+
protected function renderSelf(): void
4342
{
4443
$this->setupHighlight();
4544
$this->parse();
@@ -53,7 +52,7 @@ protected function renderSelf()
5352
}
5453

5554

56-
private function setupHighlight()
55+
private function setupHighlight(): void
5756
{
5857
ini_set('highlight.comment', 'hc');
5958
ini_set('highlight.default', 'hd');
@@ -63,7 +62,7 @@ private function setupHighlight()
6362
}
6463

6564

66-
private function parse()
65+
private function parse(): void
6766
{
6867
if (count($this->files) > 0) {
6968
return;

src/CodeCoverage/PhpParser.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
class PhpParser
2020
{
2121
/**
22-
* @param string PHP code to analyze
23-
* @return \stdClass
24-
*
2522
* Returned structure is:
2623
* stdClass {
2724
* linesOfCode: int,
@@ -52,7 +49,7 @@ class PhpParser
5249
* visibility: public|protected|private
5350
* }
5451
*/
55-
public function parse($code)
52+
public function parse(string $code): \stdClass
5653
{
5754
$tokens = @token_get_all($code); // @ - source code can be written in newer PHP
5855

@@ -167,7 +164,7 @@ public function parse($code)
167164
}
168165

169166

170-
private static function fetch(&$tokens, $take)
167+
private static function fetch(array &$tokens, $take): ?string
171168
{
172169
$res = null;
173170
while ($token = current($tokens)) {

0 commit comments

Comments
 (0)