Skip to content

Commit 0e48492

Browse files
committed
Made trait FormattedException compatible with PHP 5.4.
1 parent c31cacc commit 0e48492

File tree

9 files changed

+85
-16
lines changed

9 files changed

+85
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
22
bin/
33
composer.lock
4+
doc/
45
vendor/

build.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<target name="build">
44
</target>
55

6+
<!-- Runs ApiGen -->
7+
<target name="apigen">
8+
<exec command="apigen generate --todo --tree --source src --destination doc" passthru="true" checkreturn="true"/>
9+
</target>
10+
611
<!-- Runs all unit tests -->
712
<target name="unit">
813
<exec command="bin/phpunit --bootstrap=test/bootstrap.php test" passthru="true" checkreturn="true"/>

src/FormattedException.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,60 @@
33
namespace SetBased\Exception;
44

55
//----------------------------------------------------------------------------------------------------------------------
6+
/**
7+
* Trait for exception classes with format string in constructor.
8+
*
9+
* Example:
10+
* ```
11+
* class MyException extends \Exception
12+
* {
13+
* use FormattedException;
14+
*
15+
* public function __construct()
16+
* {
17+
* list($message, $code, $previous) = self::formattedConstruct(func_get_args());
18+
*
19+
* parent::__construct($message, $code, $previous);
20+
* }
21+
* }
22+
* ```
23+
*/
624
trait FormattedException
725
{
26+
/* PHP 5.4: doesn't allow us to use __construct in traits. This possible from PHP 5.5 and higher. */
827
//--------------------------------------------------------------------------------------------------------------------
928
/**
10-
* Object constructor.
29+
* Returns a list of arguments for \Exception::_construct.
1130
*
12-
* @param string|array $format The format string of the error message, see
13-
* [sprintf](http://php.net/manual/function.sprintf.php).
14-
* @param mixed $param,... The arguments for the format string. However, if the first argument is an exception
15-
* it will be used as the [previous](http://php.net/manual/exception.construct.php)
16-
* exception for the exception chaining.
31+
* @param \array $args The arguments for __construct.
32+
*
33+
* Below we give same samples of valid combinations for arguments for __construct.
34+
* ```
35+
* // Exception with message
36+
* new MyException('Something went wrong');
37+
*
38+
* // Exception with a format string
39+
* new MyException('There are %d monkeys in the %s', 5, 'tree');
40+
*
41+
* // Exception with an exception code
42+
* new MyException([$code], 'There are %d monkeys in the %s', 5, 'tree');
43+
*
44+
* // Exception with q previous exception
45+
* new MyException([$previous], 'There are %d monkeys in the %s', 5, 'tree');
46+
*
47+
* // Exception with an exception code and a previous exception
48+
* new MyException([$code,$previous], 'There are %d monkeys in the %s', 5, 'tree');
49+
* // or
50+
* new MyException([$previous,$code], 'There are %d monkeys in the %s', 5, 'tree');
51+
* ```
52+
*
53+
* @return \array[string,int,\Exception|null]
1754
*/
18-
public function __construct($format)
55+
public static function formattedConstruct($args)
1956
{
2057
$code = 0;
2158
$previous = null;
22-
23-
$args = func_get_args();
24-
array_shift($args);
59+
$format = array_shift($args);
2560

2661
if (is_array($format))
2762
{
@@ -41,7 +76,7 @@ public function __construct($format)
4176
}
4277
}
4378

44-
parent::__construct(vsprintf($format, $args), $code, $previous);
79+
return [vsprintf($format, $args), $code, $previous];
4580
}
4681

4782
//--------------------------------------------------------------------------------------------------------------------

src/LogicException.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44

55
//----------------------------------------------------------------------------------------------------------------------
66
/**
7-
* Parent class for errors in the program logic.
7+
* Class for errors in program logic.
88
*/
99
class LogicException extends \LogicException implements NamedException
1010
{
1111
//--------------------------------------------------------------------------------------------------------------------
1212
use FormattedException;
1313

14+
//--------------------------------------------------------------------------------------------------------------------
15+
/**
16+
* Object constructor.
17+
*
18+
* @param mixed ... The arguments, see {@see \SetBased\Exception\FormattedException::formattedConstruct}.
19+
*/
20+
public function __construct()
21+
{
22+
list($message, $code, $previous) = self::formattedConstruct(func_get_args());
23+
24+
parent::__construct($message, $code, $previous);
25+
}
26+
1427
//--------------------------------------------------------------------------------------------------------------------
1528
/**
1629
* {@inheritdoc}

src/RuntimeException.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ class RuntimeException extends \RuntimeException implements NamedException
1111
//--------------------------------------------------------------------------------------------------------------------
1212
use FormattedException;
1313

14+
//--------------------------------------------------------------------------------------------------------------------
15+
/**
16+
* Object constructor.
17+
*
18+
* See {@see \SetBased\Exception\FormattedException::formattedConstruct()} for argument list.
19+
*
20+
* @param mixed ... The arguments, see {@see \SetBased\Exception\FormattedException::formattedConstruct()}.
21+
*/
22+
public function __construct()
23+
{
24+
list($message, $code, $previous) = self::formattedConstruct(func_get_args());
25+
26+
parent::__construct($message, $code, $previous);
27+
}
28+
1429
//--------------------------------------------------------------------------------------------------------------------
1530
/**
1631
* {@inheritdoc}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use SetBased\Exception\NamedException;
44

55
//----------------------------------------------------------------------------------------------------------------------
6-
class ExceptionTestBase extends PHPUnit_Framework_TestCase
6+
class FormattedExceptionTestBase extends PHPUnit_Framework_TestCase
77
{
88
//--------------------------------------------------------------------------------------------------------------------
99
static $class;

test/LogicExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
//----------------------------------------------------------------------------------------------------------------------
3-
class LogicExceptionTest extends \ExceptionTestBase
3+
class LogicFormattedExceptionTest extends \FormattedExceptionTestBase
44
{
55
//--------------------------------------------------------------------------------------------------------------------
66
public function setUp()

test/RuntimeExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
//----------------------------------------------------------------------------------------------------------------------
3-
class RuntimeExceptionTest extends \ExceptionTestBase
3+
class RuntimeFormattedExceptionTest extends \FormattedExceptionTestBase
44
{
55
//--------------------------------------------------------------------------------------------------------------------
66
public function setUp()

test/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
date_default_timezone_set( 'Europe/Amsterdam' );
44

55
require_once( __DIR__.'/../vendor/autoload.php' );
6-
require_once('ExceptionTestBase.php' );
6+
require_once('FormattedExceptionTestBase.php' );
77

88
//----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)