Skip to content

Commit fd8c7cc

Browse files
Implement blacklist groups.
1 parent a91c322 commit fd8c7cc

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

PHP/CodeCoverage/Filter.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,18 @@ class PHP_CodeCoverage_Filter
8686
* @param string $directory
8787
* @param string $suffix
8888
* @param string $prefix
89+
* @param string $group
8990
* @throws RuntimeException
9091
*/
91-
public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '')
92+
public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
9293
{
9394
if (file_exists($directory)) {
9495
$files = File_Iterator_Factory::getFileIterator(
9596
$directory, $suffix, $prefix
9697
);
9798

9899
foreach ($files as $file) {
99-
$this->addFileToBlacklist($file->getPathName());
100+
$this->addFileToBlacklist($file->getPathName(), $group);
100101
}
101102
} else {
102103
throw new RuntimeException($directory . ' does not exist');
@@ -107,15 +108,20 @@ public function addDirectoryToBlacklist($directory, $suffix = '.php', $prefix =
107108
* Adds a new file to be filtered (blacklist).
108109
*
109110
* @param string $filename
111+
* @param string $group
110112
* @throws RuntimeException
111113
*/
112-
public function addFileToBlacklist($filename)
114+
public function addFileToBlacklist($filename, $group = 'DEFAULT')
113115
{
114116
if (file_exists($filename)) {
115117
$filename = realpath($filename);
116118

117-
if (!in_array($filename, $this->blacklistedFiles)) {
118-
$this->blacklistedFiles[] = $filename;
119+
if (!isset($this->blacklistedFiles[$group])) {
120+
$this->blacklistedFiles[$group] = array($filename);
121+
}
122+
123+
else if (!in_array($filename, $this->blacklistedFiles[$group])) {
124+
$this->blacklistedFiles[$group][] = $filename;
119125
}
120126
} else {
121127
throw new RuntimeException($filename . ' does not exist');
@@ -128,17 +134,18 @@ public function addFileToBlacklist($filename)
128134
* @param string $directory
129135
* @param string $suffix
130136
* @param string $prefix
137+
* @param string $group
131138
* @throws RuntimeException
132139
*/
133-
public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $prefix = '')
140+
public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $prefix = '', $group = 'DEFAULT')
134141
{
135142
if (file_exists($directory)) {
136143
$files = File_Iterator_Factory::getFileIterator(
137144
$directory, $suffix, $prefix
138145
);
139146

140147
foreach ($files as $file) {
141-
$this->removeFileFromBlacklist($file->getPathName());
148+
$this->removeFileFromBlacklist($file->getPathName(), $group);
142149
}
143150
} else {
144151
throw new RuntimeException($directory . ' does not exist');
@@ -149,16 +156,19 @@ public function removeDirectoryFromBlacklist($directory, $suffix = '.php', $pref
149156
* Removes a file from the filter (blacklist).
150157
*
151158
* @param string $filename
159+
* @param string $group
152160
* @throws RuntimeException
153161
*/
154-
public function removeFileFromBlacklist($filename)
162+
public function removeFileFromBlacklist($filename, $group = 'DEFAULT')
155163
{
156164
if (file_exists($filename)) {
157-
$filename = realpath($filename);
165+
if (isset($this->blacklistedFiles[$group])) {
166+
$filename = realpath($filename);
158167

159-
foreach ($this->blacklistedFiles as $key => $_filename) {
160-
if ($filename == $_filename) {
161-
unset($this->blacklistedFiles[$key]);
168+
foreach ($this->blacklistedFiles[$group] as $key => $_filename) {
169+
if ($filename == $_filename) {
170+
unset($this->blacklistedFiles[$group][$key]);
171+
}
162172
}
163173
}
164174
} else {
@@ -272,21 +282,29 @@ public static function isFile($filename)
272282

273283
/**
274284
* @param string $filename
285+
* @param array $groups
275286
* @return boolean
276287
*/
277-
public function isFiltered($filename)
288+
public function isFiltered($filename, array $groups = array('DEFAULT'))
278289
{
279290
$filename = realpath($filename);
280291

281292
if (!empty($this->whitelistedFiles)) {
282293
return !in_array($filename, $this->whitelistedFiles);
283294
}
284295

285-
if (in_array($filename, $this->blacklistedFiles)) {
286-
return TRUE;
296+
$blacklistedFiles = array();
297+
298+
foreach ($groups as $group) {
299+
if (isset($this->blacklistedFiles[$group])) {
300+
$blacklistedFiles = array_merge(
301+
$blacklistedFiles,
302+
$this->blacklistedFiles[$group]
303+
);
304+
}
287305
}
288306

289-
return FALSE;
307+
return in_array($filename, $blacklistedFiles);
290308
}
291309

292310
/**

Tests/PHP/CodeCoverage/FilterTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public function testAddingAFileToTheBlacklistWorks()
115115
$this->filter->addFileToBlacklist($this->files[0]);
116116

117117
$this->assertEquals(
118-
array($this->files[0]), $this->filter->getBlacklist()
118+
array('DEFAULT' => array($this->files[0])),
119+
$this->filter->getBlacklist()
119120
);
120121
}
121122

@@ -137,7 +138,9 @@ public function testRemovingAFileFromTheBlacklistWorks()
137138
$this->filter->addFileToBlacklist($this->files[0]);
138139
$this->filter->removeFileFromBlacklist($this->files[0]);
139140

140-
$this->assertEquals(array(), $this->filter->getBlacklist());
141+
$this->assertEquals(
142+
array('DEFAULT' => array()), $this->filter->getBlacklist()
143+
);
141144
}
142145

143146
/**
@@ -159,9 +162,12 @@ public function testAddingADirectoryToTheBlacklistWorks()
159162
$this->filter->addDirectoryToBlacklist(TEST_FILES_PATH);
160163

161164
$blacklist = $this->filter->getBlacklist();
162-
sort($blacklist);
163165

164-
$this->assertEquals($this->files, $blacklist);
166+
foreach (array_keys($blacklist) as $group) {
167+
sort($blacklist[$group]);
168+
}
169+
170+
$this->assertEquals(array('DEFAULT' => $this->files), $blacklist);
165171
}
166172

167173
/**
@@ -184,7 +190,9 @@ public function testRemovingADirectoryFromTheBlacklistWorks()
184190
$this->filter->addDirectoryToBlacklist(TEST_FILES_PATH);
185191
$this->filter->removeDirectoryFromBlacklist(TEST_FILES_PATH);
186192

187-
$this->assertEquals(array(), $this->filter->getBlacklist());
193+
$this->assertEquals(
194+
array('DEFAULT' => array()), $this->filter->getBlacklist()
195+
);
188196
}
189197

190198
/**

0 commit comments

Comments
 (0)