Skip to content

Commit ec0358a

Browse files
Merge branch 'master' into report-refactoring
2 parents 9b4d3a1 + adabe34 commit ec0358a

File tree

6 files changed

+84
-21
lines changed

6 files changed

+84
-21
lines changed

ChangeLog.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PHP_CodeCoverage 1.1.0
88

99
* Added support for merging serialized-to-disk `PHP_CodeCoverage` objects using `phpcov --merge`.
1010
* Added support for traits.
11+
* Added an option to disable the caching of `PHP_Token_Stream` objects to reduce the memory usage.
1112
* Refactored the collection and processing of code coverage data improving code readability and performance.
1213
* Refactored the generation of Clover XML and HTML reports improving code readability and testability.
1314
* Removed the support for multiple blacklist groups.

PHP/CodeCoverage.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class PHP_CodeCoverage
6767
*/
6868
protected $filter;
6969

70+
/**
71+
* @var boolean
72+
*/
73+
protected $cacheTokens = TRUE;
74+
7075
/**
7176
* @var boolean
7277
*/
@@ -333,6 +338,30 @@ public function merge(PHP_CodeCoverage $that)
333338
$this->tests = array_merge($this->tests, $that->getTests());
334339
}
335340

341+
/**
342+
* @param boolean $flag
343+
* @throws InvalidArgumentException
344+
* @since Method available since Release 1.1.0
345+
*/
346+
public function setCacheTokens($flag)
347+
{
348+
if (!is_bool($flag)) {
349+
throw new InvalidArgumentException;
350+
}
351+
352+
$this->cacheTokens = $flag;
353+
}
354+
355+
/**
356+
* @param boolean $flag
357+
* @throws InvalidArgumentException
358+
* @since Method available since Release 1.1.0
359+
*/
360+
public function getCacheTokens()
361+
{
362+
return $this->cacheTokens;
363+
}
364+
336365
/**
337366
* @param boolean $flag
338367
* @throws InvalidArgumentException
@@ -461,7 +490,12 @@ protected function processUncoveredFilesFromWhitelist()
461490
);
462491

463492
foreach ($uncoveredFiles as $uncoveredFile) {
464-
$tokens = PHP_Token_Stream_CachingFactory::get($uncoveredFile);
493+
if ($this->cacheTokens) {
494+
$tokens = PHP_Token_Stream_CachingFactory::get($uncoveredFile);
495+
} else {
496+
$tokens = new PHP_Token_Stream($uncoveredFile);
497+
}
498+
465499
$classes = $tokens->getClasses();
466500
$interfaces = $tokens->getInterfaces();
467501
$functions = $tokens->getFunctions();

PHP/CodeCoverage/Report/Factory.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public static function create(PHP_CodeCoverage $coverage)
7171
self::addItems(
7272
$root,
7373
PHP_CodeCoverage_Util::buildDirectoryStructure($files),
74-
$coverage->getTests()
74+
$coverage->getTests(),
75+
$coverage->getCacheTokens()
7576
);
7677

7778
return $root;
@@ -81,16 +82,17 @@ public static function create(PHP_CodeCoverage $coverage)
8182
* @param PHP_CodeCoverage_Report_Node_Directory $root
8283
* @param array $items
8384
* @param array $tests
85+
* @param boolean $cacheTokens
8486
*/
85-
protected static function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array $items, array $tests)
87+
protected static function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array $items, array $tests, $cacheTokens)
8688
{
8789
foreach ($items as $key => $value) {
8890
if (substr($key, -2) == '/f') {
8991
$key = substr($key, 0, -2);
90-
$root->addFile($key, $value, $tests);
92+
$root->addFile($key, $value, $tests, $cacheTokens);
9193
} else {
9294
$child = $root->addDirectory($key);
93-
self::addItems($child, $value, $tests);
95+
self::addItems($child, $value, $tests, $cacheTokens);
9496
}
9597
}
9698
}

PHP/CodeCoverage/Report/Node/Directory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,17 @@ public function addDirectory($name)
197197
/**
198198
* Adds a new file.
199199
*
200-
* @param string $name
201-
* @param array $coverageData
202-
* @param array $testData
200+
* @param string $name
201+
* @param array $coverageData
202+
* @param array $testData
203+
* @param boolean $cacheTokens
203204
* @return PHP_CodeCoverage_Report_Node_File
204205
* @throws PHP_CodeCoverage_Exception
205206
*/
206-
public function addFile($name, array $coverageData, array $testData)
207+
public function addFile($name, array $coverageData, array $testData, $cacheTokens)
207208
{
208209
$file = new PHP_CodeCoverage_Report_Node_File(
209-
$name, $this, $coverageData, $testData
210+
$name, $this, $coverageData, $testData, $cacheTokens
210211
);
211212

212213
$this->children[] = $file;

PHP/CodeCoverage/Report/Node/File.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,34 @@ class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node
136136
*/
137137
protected $endLines = array();
138138

139+
/**
140+
* @var boolean
141+
*/
142+
protected $cacheTokens;
143+
139144
/**
140145
* Constructor.
141146
*
142147
* @param string $name
143148
* @param PHP_CodeCoverage_Report_Node $parent
144149
* @param array $coverageData
145150
* @param array $testData
151+
* @param boolean $cacheTokens
146152
*/
147-
public function __construct($name, PHP_CodeCoverage_Report_Node $parent, array $coverageData, array $testData)
153+
public function __construct($name, PHP_CodeCoverage_Report_Node $parent, array $coverageData, array $testData, $cacheTokens)
148154
{
155+
if (!is_bool($cacheTokens)) {
156+
throw new InvalidArgumentException;
157+
}
158+
149159
parent::__construct($name, $parent);
150160

151161
$this->coverageData = $coverageData;
152162
$this->testData = $testData;
153163
$this->ignoredLines = PHP_CodeCoverage_Util::getLinesToBeIgnored(
154-
$this->getPath()
164+
$this->getPath(), $cacheTokens
155165
);
166+
$this->cacheTokens = $cacheTokens;
156167

157168
$this->calculateStatistics();
158169
}
@@ -383,7 +394,11 @@ public function getNumTestedFunctions()
383394
*/
384395
protected function calculateStatistics()
385396
{
386-
$tokens = PHP_Token_Stream_CachingFactory::get($this->getPath());
397+
if ($this->cacheTokens) {
398+
$tokens = PHP_Token_Stream_CachingFactory::get($this->getPath());
399+
} else {
400+
$tokens = new PHP_Token_Stream($this->getPath());
401+
}
387402

388403
$this->processClasses($tokens);
389404
$this->processTraits($tokens);

PHP/CodeCoverage/Util.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,30 @@ public static function getLinesToBeCovered($className, $methodName)
270270
/**
271271
* Returns the lines of a source file that should be ignored.
272272
*
273-
* @param string $filename
273+
* @param string $filename
274+
* @param boolean $cacheTokens
274275
* @return array
276+
* @throws InvalidArgumentException
275277
*/
276-
public static function getLinesToBeIgnored($filename)
278+
public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE)
277279
{
280+
if (!is_bool($cacheTokens)) {
281+
throw new InvalidArgumentException;
282+
}
283+
278284
if (!isset(self::$ignoredLines[$filename])) {
279285
self::$ignoredLines[$filename] = array();
286+
$ignore = FALSE;
287+
$stop = FALSE;
288+
289+
if ($cacheTokens) {
290+
$tokens = PHP_Token_Stream_CachingFactory::get($filename);
291+
} else {
292+
$tokens = new PHP_Token_Stream($filename);
293+
}
280294

281-
$ignore = FALSE;
282-
$stop = FALSE;
283-
$stream = PHP_Token_Stream_CachingFactory::get($filename);
284-
$tokens = $stream->tokens();
285-
$classes = $stream->getClasses();
286-
unset($stream);
295+
$classes = $tokens->getClasses();
296+
$tokens = $tokens->tokens();
287297

288298
foreach ($tokens as $token) {
289299
switch (get_class($token)) {

0 commit comments

Comments
 (0)