ParseExceptionLocalizer::getExceptionMessage()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 5
nop 1
1
<?php
2
3
namespace Wikibase\Repo\Localizer;
4
5
use Exception;
6
use InvalidArgumentException;
7
use Message;
8
use ValueParsers\ParseException;
9
10
/**
11
 * Provides a Message for ParseException for localized errors.
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Daniel Kinzler
15
 * @author Katie Filbert < [email protected] >
16
 */
17
class ParseExceptionLocalizer implements ExceptionLocalizer {
18
19
	/**
20
	 * @see ExceptionLocalizer::getExceptionMessage()
21
	 *
22
	 * @param Exception $exception
23
	 *
24
	 * @return Message
25
	 * @throws InvalidArgumentException
26
	 */
27
	public function getExceptionMessage( Exception $exception ) {
28
		if ( !$this->hasExceptionMessage( $exception ) ) {
29
			throw new InvalidArgumentException( '$exception is not a ParseException' );
30
		}
31
32
		$msg = null;
33
34
		/** @var ParseException $exception */
35
		'@phan-var ParseException $exception';
36
		$expectedFormat = $exception->getExpectedFormat();
37
		if ( $expectedFormat !== null ) {
38
			// Messages:
39
			// wikibase-parse-error-coordinate
40
			// wikibase-parse-error-entity-id
41
			// wikibase-parse-error-quantity
42
			// wikibase-parse-error-time
43
			$msg = new Message( 'wikibase-parse-error-' . $expectedFormat );
44
		}
45
46
		if ( !( $msg instanceof Message ) || !$msg->exists() ) {
0 ignored issues
show
Bug introduced by
The class Message does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
47
			$msg = new Message( 'wikibase-parse-error' );
48
		}
49
50
		return $msg;
51
	}
52
53
	/**
54
	 * @see ExceptionLocalizer::getExceptionMessage()
55
	 *
56
	 * @param Exception $exception
57
	 *
58
	 * @return bool
59
	 */
60
	public function hasExceptionMessage( Exception $exception ) {
61
		return $exception instanceof ParseException;
62
	}
63
64
}
65