Passed
Push — master ( 45441d...6fcca7 )
by Jeroen De
01:46
created

DimensionParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A parse() 0 13 3
A removeWhitespace() 0 3 1
1
<?php
2
3
namespace ParamProcessor\PackagePrivate;
4
5
use ValueParsers\ParseException;
6
use ValueParsers\ParserOptions;
7
use ValueParsers\ValueParser;
8
9
/**
10
 * Package private
11
 */
12
class DimensionParser implements ValueParser {
13
14
	public const DEFAULT_UNIT = 'defaultunit';
15
16
	public const PIXELS = 'px';
17
18
	private $defaultUnit;
19
20 40
	public function __construct( ParserOptions $options = null ) {
21 40
		$options = $options ?? new ParserOptions();
22
23 40
		$this->defaultUnit = $options->hasOption( self::DEFAULT_UNIT ) ? $options->getOption( self::DEFAULT_UNIT ) : self::PIXELS;
24 40
	}
25
26 40
	public function parse( $value ) {
27 40
		if ( !is_string( $value ) ) {
28 1
			throw new ParseException( 'Not a string' );
29
		}
30
31 39
		$value = $this->removeWhitespace( $value );
32
33 39
		if ( preg_match( '/^(\d|\.)+$/', $value ) ) {
34 10
			$value .= $this->defaultUnit;
35
		}
36
37 39
		return $value;
38
	}
39
40 39
	private function removeWhitespace( string $string ): string {
41 39
		return preg_replace( '/\s+/', '', $string );
42
	}
43
44
}
45