Completed
Branch 2.5/TaskLists (37fe0a)
by Josh
03:50
created

Helper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
dl 0
loc 56
ccs 18
cts 19
cp 0.9474
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A x() 0 2 1
A getStats() 0 15 3
A filterListItem() 0 22 3
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2020 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\TaskLists;
9
10
use s9e\TextFormatter\Parser;
11
use s9e\TextFormatter\Parser\Tag;
12
13
class Helper
14
{
15 5
	public static function filterListItem(Parser $parser, Tag $listItem, string $text): void
16
	{
17
		// Test whether the list item is followed by a task checkbox
18 5
		$pos  = $listItem->getPos() + $listItem->getLen();
19 5
		$pos += strspn($text, ' ', $pos);
20 5
		$str  = substr($text, $pos, 3);
21 5
		if (!preg_match('/\\[[ Xx]\\]/', $str))
22
		{
23
			return;
24
		}
25
26
		// Create a tag for the task and assign it a random ID
27 5
		$taskId = uniqid();
28 5
		$task   = $parser->addSelfClosingTag('TASK', $pos, 3);
29 5
		$task->setAttribute('id', $taskId);
30 5
		if ($str !== '[ ]')
31
		{
32 5
			$task->setAttribute('state', 'checked');
33
		}
34
35
		// Copy the task's ID to the list item
36 5
		$listItem->setAttribute('task_id', $taskId);
37
	}
38
39
	/**
40
	* 
41
	*
42
	* @return void
43
	*/
44
	public static function x()
45
	{
46
	}
47
48
	/**
49
	* Return stats from a parsed representation
50
	*
51
	* @param  string $xml Parsed XML
52
	* @return array       Number of "checked" and "unchecked" tasks
53
	*/
54 2
	public static function getStats(string $xml): array
55
	{
56
		$stats = [
57 2
			'checked'   => 0,
58
			'unchecked' => 0
59
		];
60
61 2
		preg_match_all('(<TASK(?= )[^>]++)', $xml, $m);
62 2
		foreach ($m[0] as $str)
63
		{
64 1
			$state = (strpos($str, 'state="checked"')) ? 'checked' : 'unchecked';
65 1
			++$stats[$state];
66
		}
67
68 2
		return $stats;
69
	}
70
}