Skip to content

Commit aaad35b

Browse files
Improve exception messages.
1 parent e930443 commit aaad35b

File tree

9 files changed

+138
-34
lines changed

9 files changed

+138
-34
lines changed

PHP/CodeCoverage.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ class PHP_CodeCoverage
109109
/**
110110
* Constructor.
111111
*
112-
* @param PHP_CodeCoverage_Driver $driver
113-
* @param PHP_CodeCoverage_Filter $filter
114-
* @throws InvalidArgumentException
112+
* @param PHP_CodeCoverage_Driver $driver
113+
* @param PHP_CodeCoverage_Filter $filter
115114
*/
116115
public function __construct(PHP_CodeCoverage_Driver $driver = NULL, PHP_CodeCoverage_Filter $filter = NULL)
117116
{
@@ -196,12 +195,14 @@ public function getTests()
196195
*
197196
* @param mixed $id
198197
* @param boolean $clear
199-
* @throws InvalidArgumentException
198+
* @throws PHP_CodeCoverage_Exception
200199
*/
201200
public function start($id, $clear = FALSE)
202201
{
203202
if (!is_bool($clear)) {
204-
throw new InvalidArgumentException;
203+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
204+
1, 'boolean'
205+
);
205206
}
206207

207208
if ($clear) {
@@ -218,12 +219,14 @@ public function start($id, $clear = FALSE)
218219
*
219220
* @param boolean $append
220221
* @return array
221-
* @throws InvalidArgumentException
222+
* @throws PHP_CodeCoverage_Exception
222223
*/
223224
public function stop($append = TRUE)
224225
{
225226
if (!is_bool($append)) {
226-
throw new InvalidArgumentException;
227+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
228+
1, 'boolean'
229+
);
227230
}
228231

229232
$data = $this->driver->stop();
@@ -248,7 +251,7 @@ public function append(array $data, $id = NULL, $append = TRUE)
248251
}
249252

250253
if ($id === NULL) {
251-
throw new InvalidArgumentException;
254+
throw new PHP_CodeCoverage_Exception;
252255
}
253256

254257
$this->applyListsFilter($data);
@@ -326,22 +329,23 @@ public function merge(PHP_CodeCoverage $that)
326329

327330
/**
328331
* @param boolean $flag
329-
* @throws InvalidArgumentException
332+
* @throws PHP_CodeCoverage_Exception
330333
* @since Method available since Release 1.1.0
331334
*/
332335
public function setCacheTokens($flag)
333336
{
334337
if (!is_bool($flag)) {
335-
throw new InvalidArgumentException;
338+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
339+
1, 'boolean'
340+
);
336341
}
337342

338343
$this->cacheTokens = $flag;
339344
}
340345

341346
/**
342-
* @param boolean $flag
343-
* @throws InvalidArgumentException
344-
* @since Method available since Release 1.1.0
347+
* @param boolean $flag
348+
* @since Method available since Release 1.1.0
345349
*/
346350
public function getCacheTokens()
347351
{
@@ -350,38 +354,44 @@ public function getCacheTokens()
350354

351355
/**
352356
* @param boolean $flag
353-
* @throws InvalidArgumentException
357+
* @throws PHP_CodeCoverage_Exception
354358
*/
355359
public function setForceCoversAnnotation($flag)
356360
{
357361
if (!is_bool($flag)) {
358-
throw new InvalidArgumentException;
362+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
363+
1, 'boolean'
364+
);
359365
}
360366

361367
$this->forceCoversAnnotation = $flag;
362368
}
363369

364370
/**
365371
* @param boolean $flag
366-
* @throws InvalidArgumentException
372+
* @throws PHP_CodeCoverage_Exception
367373
*/
368374
public function setMapTestClassNameToCoveredClassName($flag)
369375
{
370376
if (!is_bool($flag)) {
371-
throw new InvalidArgumentException;
377+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
378+
1, 'boolean'
379+
);
372380
}
373381

374382
$this->mapTestClassNameToCoveredClassName = $flag;
375383
}
376384

377385
/**
378386
* @param boolean $flag
379-
* @throws InvalidArgumentException
387+
* @throws PHP_CodeCoverage_Exception
380388
*/
381389
public function setProcessUncoveredFilesFromWhitelist($flag)
382390
{
383391
if (!is_bool($flag)) {
384-
throw new InvalidArgumentException;
392+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
393+
1, 'boolean'
394+
);
385395
}
386396

387397
$this->processUncoveredFilesFromWhitelist = $flag;

PHP/CodeCoverage/Autoload.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function php_codecoverage_autoload($class = NULL) {
7171
'php_codecoverage_report_node_iterator' => '/CodeCoverage/Report/Node/Iterator.php',
7272
'php_codecoverage_report_php' => '/CodeCoverage/Report/PHP.php',
7373
'php_codecoverage_report_text' => '/CodeCoverage/Report/Text.php',
74-
'php_codecoverage_util' => '/CodeCoverage/Util.php'
74+
'php_codecoverage_util' => '/CodeCoverage/Util.php',
75+
'php_codecoverage_util_invalidargumenthelper' => '/CodeCoverage/Util/InvalidArgumentHelper.php'
7576
);
7677

7778
$path = dirname(dirname(__FILE__));

PHP/CodeCoverage/Filter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,14 @@ public function isFile($filename)
292292
* @param string $filename
293293
* @param boolean $ignoreWhitelist
294294
* @return boolean
295-
* @throws InvalidArgumentException
295+
* @throws PHP_CodeCoverage_Exception
296296
*/
297297
public function isFiltered($filename, $ignoreWhitelist = FALSE)
298298
{
299299
if (!is_bool($ignoreWhitelist)) {
300-
throw new InvalidArgumentException;
300+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
301+
1, 'boolean'
302+
);
301303
}
302304

303305
$filename = realpath($filename);

PHP/CodeCoverage/Report/Node/File.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,14 @@ class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node
150150
* @param array $coverageData
151151
* @param array $testData
152152
* @param boolean $cacheTokens
153+
* @throws PHP_CodeCoverage_Exception
153154
*/
154155
public function __construct($name, PHP_CodeCoverage_Report_Node $parent, array $coverageData, array $testData, $cacheTokens)
155156
{
156157
if (!is_bool($cacheTokens)) {
157-
throw new InvalidArgumentException;
158+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
159+
1, 'boolean'
160+
);
158161
}
159162

160163
parent::__construct($name, $parent);

PHP/CodeCoverage/Util.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,14 @@ public static function getLinesToBeCovered($className, $methodName)
227227
* @param string $filename
228228
* @param boolean $cacheTokens
229229
* @return array
230-
* @throws InvalidArgumentException
230+
* @throws PHP_CodeCoverage_Exception
231231
*/
232232
public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE)
233233
{
234234
if (!is_bool($cacheTokens)) {
235-
throw new InvalidArgumentException;
235+
throw PHP_CodeCoverage_Util_InvalidArgumentHelper::factory(
236+
1, 'boolean'
237+
);
236238
}
237239

238240
if (!isset(self::$ignoredLines[$filename])) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* PHP_CodeCoverage
4+
*
5+
* Copyright (c) 2009-2012, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @category PHP
38+
* @package CodeCoverage
39+
* @author Sebastian Bergmann <[email protected]>
40+
* @copyright 2009-2012 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42+
* @link http://github.com/sebastianbergmann/php-code-coverage
43+
* @since File available since Release 1.2.0
44+
*/
45+
46+
/**
47+
* Factory for PHP_CodeCoverage_Exception objects that are used to describe
48+
* invalid arguments passed to a function or method.
49+
*
50+
* @category PHP
51+
* @package CodeCoverage
52+
* @author Sebastian Bergmann <[email protected]>
53+
* @copyright 2009-2012 Sebastian Bergmann <[email protected]>
54+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
55+
* @version Release: @package_version@
56+
* @link http://github.com/sebastianbergmann/php-code-coverage
57+
* @since Class available since Release 1.2.0
58+
*/
59+
class PHP_CodeCoverage_Util_InvalidArgumentHelper
60+
{
61+
/**
62+
* @param integer $argument
63+
* @param string $type
64+
* @param mixed $value
65+
*/
66+
public static function factory($argument, $type, $value = NULL)
67+
{
68+
$stack = debug_backtrace(FALSE);
69+
70+
return new PHP_CodeCoverage_Exception(
71+
sprintf(
72+
'Argument #%d%sof %s::%s() must be a %s',
73+
$argument,
74+
$value !== NULL ? ' (' . $value . ')' : ' ',
75+
$stack[1]['class'],
76+
$stack[1]['function'],
77+
$type
78+
)
79+
);
80+
}
81+
}

Tests/PHP/CodeCoverage/FilterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function testNotWhitelistedFileIsFiltered()
296296

297297
/**
298298
* @covers PHP_CodeCoverage_Filter::isFiltered
299-
* @expectedException InvalidArgumentException
299+
* @expectedException PHP_CodeCoverage_Exception
300300
*/
301301
public function testIsFilteredThrowsExceptionForInvalidArgument()
302302
{

Tests/PHP/CodeCoverageTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testConstructor2()
107107

108108
/**
109109
* @covers PHP_CodeCoverage::start
110-
* @expectedException InvalidArgumentException
110+
* @expectedException PHP_CodeCoverage_Exception
111111
*/
112112
public function testStartThrowsExceptionForInvalidArgument()
113113
{
@@ -117,7 +117,7 @@ public function testStartThrowsExceptionForInvalidArgument()
117117

118118
/**
119119
* @covers PHP_CodeCoverage::stop
120-
* @expectedException InvalidArgumentException
120+
* @expectedException PHP_CodeCoverage_Exception
121121
*/
122122
public function testStopThrowsExceptionForInvalidArgument()
123123
{
@@ -127,7 +127,7 @@ public function testStopThrowsExceptionForInvalidArgument()
127127

128128
/**
129129
* @covers PHP_CodeCoverage::append
130-
* @expectedException InvalidArgumentException
130+
* @expectedException PHP_CodeCoverage_Exception
131131
*/
132132
public function testAppendThrowsExceptionForInvalidArgument()
133133
{
@@ -137,7 +137,7 @@ public function testAppendThrowsExceptionForInvalidArgument()
137137

138138
/**
139139
* @covers PHP_CodeCoverage::setCacheTokens
140-
* @expectedException InvalidArgumentException
140+
* @expectedException PHP_CodeCoverage_Exception
141141
*/
142142
public function testSetCacheTokensThrowsExceptionForInvalidArgument()
143143
{
@@ -157,7 +157,7 @@ public function testSetCacheTokens()
157157

158158
/**
159159
* @covers PHP_CodeCoverage::setForceCoversAnnotation
160-
* @expectedException InvalidArgumentException
160+
* @expectedException PHP_CodeCoverage_Exception
161161
*/
162162
public function testSetForceCoversAnnotationThrowsExceptionForInvalidArgument()
163163
{
@@ -177,7 +177,7 @@ public function testSetForceCoversAnnotation()
177177

178178
/**
179179
* @covers PHP_CodeCoverage::setProcessUncoveredFilesFromWhitelist
180-
* @expectedException InvalidArgumentException
180+
* @expectedException PHP_CodeCoverage_Exception
181181
*/
182182
public function testSetProcessUncoveredFilesFromWhitelistThrowsExceptionForInvalidArgument()
183183
{
@@ -211,7 +211,7 @@ public function testSetMapTestClassNameToCoveredClassName()
211211

212212
/**
213213
* @covers PHP_CodeCoverage::setMapTestClassNameToCoveredClassName
214-
* @expectedException InvalidArgumentException
214+
* @expectedException PHP_CodeCoverage_Exception
215215
*/
216216
public function testSetMapTestClassNameToCoveredClassNameThrowsExceptionForInvalidArgument()
217217
{

package.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@
104104
<tasks:replace from="@package_version@" to="version" type="package-info" />
105105
</file>
106106
</dir>
107+
<dir name="Util">
108+
<file baseinstalldir="/" name="InvalidArgumentHelper.php" role="php">
109+
<tasks:replace from="@package_version@" to="version" type="package-info" />
110+
</file>
111+
</dir>
107112
<file baseinstalldir="/" name="Autoload.php" role="php">
108113
<tasks:replace from="@package_version@" to="version" type="package-info" />
109114
</file>
@@ -169,4 +174,4 @@
169174
</optional>
170175
</dependencies>
171176
<phprelease/>
172-
</package>
177+
</package>

0 commit comments

Comments
 (0)