Completed
Push — master ( 92ba79...be2618 )
by Josh
13:33
created

AbstractScript::parseLongForm()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.0534
c 1
b 0
f 0
cc 4
eloc 11
nc 5
nop 1
crap 4
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Litedown\Parser\Passes;
9
10
abstract class AbstractScript extends AbstractPass
11
{
12
	/**
13
	* @var string $longRegexp Regexp used for the long form syntax
14
	*/
15
	protected $longRegexp;
16
17
	/**
18
	* @var string Regexp used for the short form syntax
19
	*/
20
	protected $shortRegexp;
21
22
	/**
23
	* @var string Relevant character used by this syntax
24
	*/
25
	protected $syntaxChar;
26
	
27
	/**
28
	* @var string Name of the tag used by this pass
29
	*/
30
	protected $tagName;
31
32
	/**
33
	* @param  string $tagName     Name of the tag used by this pass
34
	* @param  string $syntaxChar  Relevant character used by this syntax
35
	* @param  string $shortRegexp Regexp used for the short form syntax
36
	* @param  string $longRegexp  Regexp used for the long form syntax
37
	* @return void
38
	*/
39 10
	protected function parseAbstractScript($tagName, $syntaxChar, $shortRegexp, $longRegexp)
40
	{
41 10
		$this->tagName     = $tagName;
42 10
		$this->syntaxChar  = $syntaxChar;
43 10
		$this->shortRegexp = $shortRegexp;
44 10
		$this->longRegexp  = $longRegexp;
45
46 10
		$pos = $this->text->indexOf($this->syntaxChar);
47 10
		if ($pos === false)
48
		{
49 1
			return;
50
		}
51
52 9
		$this->parseShortForm($pos);
1 ignored issue
show
Bug introduced by
It seems like $pos defined by $this->text->indexOf($this->syntaxChar) on line 46 can also be of type boolean; however, s9e\TextFormatter\Plugin...cript::parseShortForm() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53 9
		$this->parseLongForm($pos);
1 ignored issue
show
Bug introduced by
It seems like $pos defined by $this->text->indexOf($this->syntaxChar) on line 46 can also be of type boolean; however, s9e\TextFormatter\Plugin...Script::parseLongForm() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54 9
	}
55
56
	/**
57
	* Parse the long form x^(x)
58
	*
59
	* This syntax is supported by RDiscount
60
	*
61
	* @param  integer $pos Position of the first relevant character
62
	* @return void
63
	*/
64 9
	protected function parseLongForm($pos)
65
	{
66 9
		$pos = $this->text->indexOf($this->syntaxChar . '(', $pos);
67 9
		if ($pos === false)
68
		{
69 7
			return;
70
		}
71
72 5
		preg_match_all($this->longRegexp, $this->text, $matches, PREG_OFFSET_CAPTURE, $pos);
73 5
		foreach ($matches[0] as list($match, $matchPos))
74
		{
75 3
			$matchLen = strlen($match);
76
77 3
			$this->parser->addTagPair($this->tagName, $matchPos, 2, $matchPos + $matchLen - 1, 1);
78 3
			$this->text->overwrite($matchPos, $matchLen);
79
		}
80 5
		if (!empty($matches[0]))
81
		{
82 3
			$this->parseLongForm($pos);
1 ignored issue
show
Bug introduced by
It seems like $pos defined by $this->text->indexOf($th...syntaxChar . '(', $pos) on line 66 can also be of type boolean; however, s9e\TextFormatter\Plugin...Script::parseLongForm() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
		}
84 5
	}
85
86
	/**
87
	* Parse the short form x^x and x^x^
88
	*
89
	* This syntax is supported by most implementations that support superscript
90
	*
91
	* @param  integer $pos Position of the first relevant character
92
	* @return void
93
	*/
94 9
	protected function parseShortForm($pos)
95
	{
96 9
		preg_match_all($this->shortRegexp, $this->text, $matches, PREG_OFFSET_CAPTURE, $pos);
97 9
		foreach ($matches[0] as list($match, $matchPos))
98
		{
99 5
			$matchLen = strlen($match);
100 5
			$startPos = $matchPos;
101 5
			$endLen   = (substr($match, -1) === $this->syntaxChar) ? 1 : 0;
102 5
			$endPos   = $matchPos + $matchLen - $endLen;
103
104 5
			$this->parser->addTagPair($this->tagName, $startPos, 1, $endPos, $endLen);
105
		}
106
	}
107
}