Skip to content

Commit 77f8bb2

Browse files
Merge pull request #21 from php-enspired/analysis+tests
Tests + Analysis
2 parents 023fded + f4deb6c commit 77f8bb2

File tree

8 files changed

+50
-28
lines changed

8 files changed

+50
-28
lines changed

.phan/config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'directory_list' => ['src', 'vendor'],
5+
'exclude_analysis_directory_list' => ['vendor']
6+
];

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
"php": "^7.1"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^5.7"
21+
"phan/phan": "^1",
22+
"phpunit/phpunit": "^7"
2223
},
2324
"autoload": {
2425
"psr-4": {
2526
"at\\exceptable\\": "src/"
2627
}
2728
},
2829
"scripts": {
29-
"test:unit" : "vendor/bin/phpunit --bootstrap vendor/autoload.php tests"
30+
"test:phan": "vendor/bin/phan",
31+
"test:unit" : "vendor/bin/phpunit tests"
3032
}
3133
}

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="./vendor/autoload.php"
4+
colors="true">
5+
<filter>
6+
<whitelist processUncoveredFilesFromWhitelist="true">
7+
<directory suffix=".php">./src</directory>
8+
</whitelist>
9+
</filter>
10+
</phpunit>

src/Exceptable.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
use Throwable;
2424

25+
use at\exceptable\ExceptableException;
26+
2527
/**
2628
* augmented interface for exceptions.
2729
*
@@ -49,9 +51,9 @@ interface Exceptable extends Throwable {
4951
/**
5052
* gets information about a code known to the implementing class.
5153
*
52-
* @param int $code the exception code to look up
53-
* @throws Exceptable if the code is not known to the implementation
54-
* @return array a map of info about the error condition
54+
* @param int $code the exception code to look up
55+
* @throws ExceptableException if the code is not known to the implementation
56+
* @return array a map of info about the error condition
5557
*/
5658
public static function getInfo(int $code) : array;
5759

@@ -64,10 +66,10 @@ public static function getInfo(int $code) : array;
6466
public static function hasInfo(int $code) : bool;
6567

6668
/**
67-
* @param int $code exception code
68-
* @param array $context additional exception context
69-
* @param Throwable $previous previous exception
70-
* @throws ExceptableException if code is invalid
69+
* @param int $code exception code
70+
* @param array $context additional exception context
71+
* @param Throwable|null $previous previous exception
72+
* @throws ExceptableException if code is invalid
7173
*/
7274
public function __construct(int $code, array $context = [], Throwable $previous = null);
7375

src/Exception.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
namespace at\exceptable;
2222

23-
use Exception as BaseException,
24-
Throwable;
23+
use Exception as BaseException;
2524

2625
use at\exceptable\ {
2726
Exceptable,

src/Handler.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use ErrorException,
2424
Throwable;
25+
2526
use at\exceptable\Exceptable;
2627

2728
class Handler {
@@ -44,9 +45,9 @@ class Handler {
4445
/**
4546
* registers this handler to invoke a callback, and then restores the previous handler(s).
4647
*
47-
* @param callable $callback the callback to execute
48-
* @param mixed $arguments arguments to pass to the callback
49-
* @return mixed the value returned from the callback
48+
* @param callable $callback the callback to execute
49+
* @param mixed ...$arguments arguments to pass to the callback
50+
* @return mixed the value returned from the callback
5051
*/
5152
public function during(callable $callback, ...$arguments) {
5253
$registered = $this->_registered;
@@ -68,7 +69,7 @@ public function during(callable $callback, ...$arguments) {
6869
* @see <http://php.net/set_error_handler> $error_handler
6970
*
7071
* @param callable $handler the error handler to add
71-
* @param int $types the error type(s) to trigger this handler
72+
* @param int|null $types the error type(s) to trigger this handler
7273
* (bitmask of E_* constants, or omit for "any severity")
7374
* @return Handler $this
7475
*/
@@ -82,7 +83,7 @@ public function onError(callable $handler, int $types=null) : Handler {
8283
* @see <http://php.net/set_exception_handler> $exception_handler
8384
*
8485
* @param callable $handler the exception handler to add
85-
* @param int $severity the exception severity to trigger this handler
86+
* @param int|null $severity the exception severity to trigger this handler
8687
* (one of Exceptable::ERROR|WARNING|NOTICE, or omit for "any severity")
8788
* @return Handler $this
8889
*/
@@ -96,7 +97,7 @@ public function onException(callable $handler, int $severity=null) : Handler {
9697
* @see <http:/php.net/register_shutdown_handler> $callback
9798
*
9899
* @param callable $handler the shutdown handler to add
99-
* @param mixed $arguments optional agrs to pass to shutdown handler when invoked
100+
* @param mixed ...$arguments optional agrs to pass to shutdown handler when invoked
100101
* @return Handler $this
101102
*/
102103
public function onShutdown(callable $handler, ...$arguments) : Handler {
@@ -135,7 +136,7 @@ public function throw(int $types=E_ERROR|E_WARNING) : Handler {
135136
* tries invoking a callback, handling any exceptions using registered handlers.
136137
*
137138
* @param callable $callback the callback to execute
138-
* @param mixed $arguments arguments to pass to the callback
139+
* @param mixed ...$arguments arguments to pass to the callback
139140
* @throws ExceptableException if no registered handler handles the exception
140141
* @return mixed the value returned from the callback
141142
*/
@@ -191,7 +192,7 @@ protected function _error(int $s, string $m, string $f, int $l, array $c) : bool
191192
* @throws ExceptableException if no registered handler handles the exception
192193
*/
193194
protected function _exception(Throwable $e) {
194-
$severity = method_exists($e, 'getSeverity') ? $e->getSeverity() : Exceptable::ERROR;
195+
$severity = $e instanceof Exceptable ? $e->getSeverity() : Exceptable::ERROR;
195196
foreach ($this->_exceptionHandlers as $handler) {
196197
if ($handler->handles(_Handler::TYPE_EXCEPTION, $severity) && $handler->handle($e)) {
197198
return;
@@ -250,8 +251,8 @@ public function handle(...$arguments) : bool {
250251
} catch (Throwable $e) {
251252
throw new ExceptableException(
252253
ExceptableException::INVALID_HANDLER,
253-
$e,
254-
['type' => $this->_type()]
254+
['type' => $this->_type()],
255+
$e
255256
);
256257
}
257258
}

src/HasContext.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@
2222

2323
use Throwable;
2424

25-
use at\exceptable\ {
26-
Exceptable,
27-
ExceptableException
28-
};
29-
3025
/**
3126
* base implementation for Exceptable interface, including contexted message construction.
27+
*
28+
* this trait MUST be used by a class which implements Throwable.
3229
*/
3330
trait HasContext {
3431

@@ -42,6 +39,9 @@ trait HasContext {
4239
/** {@inheritDoc} */
4340
abstract public static function getInfo(int $code) : array;
4441

42+
/** @see https://php.net/Throwable.getPrevious */
43+
abstract public function getPrevious();
44+
4545
/** {@inheritDoc} */
4646
abstract public static function hasInfo(int $code) : bool;
4747

@@ -53,17 +53,20 @@ public function __construct(int $code, array $context = [], Throwable $previous
5353
$context['__severity__'] = $this->_severity = $info['severity'];
5454
$this->_context = $context;
5555

56+
// @phan-suppress-next-line PhanTraitParentReference
5657
parent::__construct($this->_makeMessage($code) ?? $info['message'], $code, $previous);
5758
}
5859

5960
/** @see https://php.net/__toString */
6061
public function __toString() {
6162
try {
63+
// @phan-suppress-next-line PhanTraitParentReference
6264
return parent::__toString() . "\ncontext: " . json_encode(
6365
$this->getContext(),
6466
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
6567
);
6668
} catch (Throwable $e) {
69+
// @phan-suppress-next-line PhanTraitParentReference
6770
return parent::__toString();
6871
}
6972
}

tests/ExceptableTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public function testGetInfo() {
4949
$this->assertEquals($code, $actual['code']);
5050
$this->assertContains(
5151
$actual['severity'],
52-
[Exceptable::ERROR, Exceptable::WARNING, Exceptable::NOTICE],
53-
true
52+
[Exceptable::ERROR, Exceptable::WARNING, Exceptable::NOTICE]
5453
);
5554
$this->assertInternalType('string', $actual['message']);
5655
}

0 commit comments

Comments
 (0)