ExceptionFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 0
cts 31
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatForAnnotation() 0 37 3
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/signals
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/signals/
11
 */
12
13
namespace Maslosoft\Signals\Helpers;
14
15
use Maslosoft\Signals\Meta\SignalsAnnotation;
16
use ReflectionClass;
17
18
/**
19
 * ExceptionFormatter
20
 *
21
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
22
 */
23
class ExceptionFormatter
24
{
25
26
	public static function formatForAnnotation(SignalsAnnotation $annotation, $class)
27
	{
28
		$shortName = (new ReflectionClass($annotation))->getShortName();
29
		$type = preg_replace('~Annotation$~', '', $shortName);
30
		$typeName = $annotation->getMeta()->type()->name;
31
		$name = $annotation->name;
32
33
		// Always available params
34
		$params = [
35
			$type,
36
			$typeName,
37
		];
38
39
		// Prepare first part of message
40
		if (empty($class))
41
		{
42
			$msg = 'Could not resolve class name';
43
		}
44
		else
45
		{
46
			array_unshift($params, $class);
47
			$msg = 'Class `%s` not found';
48
		}
49
50
		// Prepare location of bogus annotation
51
		if ($name === $typeName)
52
		{
53
			$msgOn = 'for `@%s` annotation on `%s`';
54
		}
55
		else
56
		{
57
			$params[] = $name;
58
			$msgOn = 'for `@%s` annotation on `%s::%s`';
59
		}
60
		$msg = implode(' ', [$msg, $msgOn]);
61
		return vsprintf($msg, $params);
62
	}
63
64
}
65