Completed
Pull Request — master (#130)
by Hisateru
03:32
created

Markdown.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @copyright Copyright (c) 2014 Carsten Brandt
4
 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5
 * @link https://github.com/cebe/markdown#readme
6
 */
7
8
namespace cebe\markdown;
9
10
/**
11
 * Markdown parser for the [initial markdown spec](http://daringfireball.net/projects/markdown/syntax).
12
 *
13
 * @author Carsten Brandt <[email protected]>
14
 */
15
class Markdown extends Parser
16
{
17
	// include block element parsing using traits
18
	use block\CodeTrait;
19
	use block\HeadlineTrait;
20
	use block\HtmlTrait {
21
		parseInlineHtml as private;
22
	}
23
	use block\ListTrait {
24
		// Check Ul List before headline
25
		identifyUl as protected identifyBUl;
26
		consumeUl as protected consumeBUl;
27
	}
28
	use block\QuoteTrait;
29
	use block\RuleTrait {
30
		// Check Hr before checking lists
31
		identifyHr as protected identifyAHr;
32
		consumeHr as protected consumeAHr;
33
	}
34
35
	// include inline element parsing using traits
36
	use inline\CodeTrait;
37
	use inline\EmphStrongTrait;
38
	use inline\LinkTrait;
39
40
	/**
41
	 * @var boolean whether to format markup according to HTML5 spec.
42
	 * Defaults to `false` which means that markup is formatted as HTML4.
43
	 */
44
	public $html5 = false;
45
46
	/**
47
	 * @var array these are "escapeable" characters. When using one of these prefixed with a
48
	 * backslash, the character will be outputted without the backslash and is not interpreted
49
	 * as markdown.
50
	 */
51
	protected $escapeCharacters = [
52
		'\\', // backslash
53
		'`', // backtick
54
		'*', // asterisk
55
		'_', // underscore
56
		'{', '}', // curly braces
57
		'[', ']', // square brackets
58
		'(', ')', // parentheses
59
		'#', // hash mark
60
		'+', // plus sign
61
		'-', // minus sign (hyphen)
62
		'.', // dot
63
		'!', // exclamation mark
64
		'<', '>',
65
	];
66
67
68
	/**
69
	 * @inheritDoc
70
	 */
71 195
	protected function prepare()
72
	{
73
		// reset references
74 195
		$this->references = [];
75 195
	}
76
77
	/**
78
	 * Consume lines for a paragraph
79
	 *
80
	 * Allow headlines and code to break paragraphs
81
	 */
82 122
	protected function consumeParagraph($lines, $current)
83
	{
84
		// consume until newline
85 122
		$content = [];
86 122
		for ($i = $current, $count = count($lines); $i < $count; $i++) {
87 122
			$line = $lines[$i];
88
89
			// a list may break a paragraph when it is inside of a list
90 122
			if (isset($this->context[1]) && $this->context[1] === 'list' && !ctype_alpha($line[0]) && (
91 122
				$this->identifyUl($line, $lines, $i) || $this->identifyOl($line, $lines, $i))) {
92
				break;
93
			}
94
95 122
			if (
96 102
				$line === '' ||
97 122
				ltrim($line) === '' ||
98
				!ctype_alpha($line[0]) && (
99
					$this->identifyQuote($line, $lines, $i) ||
0 ignored issues
show
The call to Markdown::identifyQuote() has too many arguments starting with $lines.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
					$this->identifyHr($line, $lines, $i) ||
0 ignored issues
show
The call to Markdown::identifyHr() has too many arguments starting with $lines.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
					$this->identifyHtml($line, $lines, $i)
102 4
				)
103 2
				|| $this->identifyHeadline($line, $lines, $i)) {
104 2
				break;
105 4 View Code Duplication
			} elseif ($this->identifyCode($line, $lines, $i)) {
0 ignored issues
show
The call to Markdown::identifyCode() has too many arguments starting with $lines.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
				// possible beginning of a code block
107 2
				// but check for continued inline HTML
108 122
				// e.g. <img src="file.jpg"
109
				//           alt="some alt aligned with src attribute" title="some text" />
110 122
				if (preg_match('~<\w+([^>]+)$~s', implode("\n", $content))) {
111
					$content[] = $line;
112 122
				} else {
113 122
					break;
114 122
				}
115 122
			} else {
116
				$content[] = $line;
117
			}
118
		}
119
		$block = [
120
			'paragraph',
121
			'content' => $this->parseInline(implode("\n", $content)),
122
		];
123
		return [$block, --$i];
124 194
	}
125
126 194
127
	/**
128
	 * @inheritdocs
129
	 *
130
	 * Parses a newline indicated by two spaces on the end of a markdown line.
131
	 */
132
	protected function renderText($text)
133
	{
134
		return str_replace("  \n", $this->html5 ? "<br>\n" : "<br />\n", $text[1]);
135
	}
136
}
137