Completed
Push — master ( 65c29b...b1501d )
by Josh
02:11
created

PrintableAscii::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 4
nop 1
crap 3
1
<?php
2
3
/**
4
* @package   s9e\RegexpBuilder
5
* @copyright Copyright (c) 2016 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\RegexpBuilder\Output;
9
10
abstract class PrintableAscii extends BaseImplementation
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 34
	public function __construct(array $options = [])
16
	{
17 34
		$this->hexCase = (isset($options['case']) && $options['case'] === 'lower') ? 'x' : 'X';
0 ignored issues
show
Bug introduced by
The property hexCase does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18 34
	}
19
20
	/**
21
	* Escape given ASCII codepoint
22
	*
23
	* @param  integer $cp
24
	* @return string
25
	*/
26 6
	protected function escapeAscii($cp)
27
	{
28 6
		return '\\x' . sprintf('%02' . $this->hexCase, $cp);
29
	}
30
31
	/**
32
	* Escape given control code
33
	*
34
	* @param  integer $cp
35
	* @return string
36
	*/
37 6
	protected function escapeControlCode($cp)
38
	{
39 6
		$table = [9 => '\\t', 10 => '\\n', 13 => '\\r'];
40
41 6
		return (isset($table[$cp])) ? $table[$cp] : $this->escapeAscii($cp);
42
	}
43
44
	/**
45
	* Output the representation of a unicode character
46
	*
47
	* @param  integer $cp Unicode codepoint
48
	* @return string
49
	*/
50
	abstract protected function escapeUnicode($cp);
51
52
	/**
53
	* {@inheritdoc}
54
	*/
55 32
	protected function outputValidValue($value)
56
	{
57 32
		if ($value < 32)
58
		{
59 6
			return $this->escapeControlCode($value);
60
		}
61
62 26
		if ($value < 127)
63
		{
64 6
			return chr($value);
65
		}
66
67 20
		return ($value > 255) ? $this->escapeUnicode($value) : $this->escapeAscii($value);
68
	}
69
}