GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ManualTitleLabelsPlugin::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the trefoil application.
4
 *
5
 * (c) Miguel Angel Gabriel <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Trefoil\Plugins\Optional;
11
12
use Easybook\Events\EasybookEvents;
13
use Easybook\Events\ParseEvent;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Trefoil\Plugins\BasePlugin;
16
17
/**
18
 * Add manual title labels to book items.
19
 *
20
 * For formats: all
21
 *
22
 * Item labels are rendered with different markup than the title, creating a nice effect,
23
 * like "Chapter 1 - The first chapter title" ("Chapter 1" is the label while "The first
24
 * chapter title" is the title).
25
 * 
26
 * Automatic labels can be added to item titles by easybook using its labeling mechanism, 
27
 * but sometimes it could be useful having a way to manually specify labels using markup
28
 * in the source file.
29
 *
30
 * This plugin provides simple markup to achieve just this, just enclosing the label part
31
 * in "[[..]]".
32
 *
33
 * Example:
34
 * 
35
 *   # [[Chapter 1]] The first chapter title
36
 * 
37
 */
38
class ManualTitleLabelsPlugin extends BasePlugin implements EventSubscriberInterface
39
{
40 1
    public static function getSubscribedEvents()
41
    {
42
        return array(
43 1
            EasybookEvents::POST_PARSE => array('onItemPostParse', -1100) // apter ParserPlugin
44 1
        );
45
    }
46
47 1
    public function onItemPostParse(ParseEvent $event)
48
    {
49 1
        $this->init($event);
50
51 1
        $this->addTitleLabel();
52
        
53 1
        $event->setItem($this->item);
54 1
    }
55
56
    /**
57
     * Look for '[[label]] title' markup in title and update 
58
     * the item accordingly.
59
     */
60 1
    protected function addTitleLabel()
61
    {
62 1
        $regExp = '/';
63 1
        $regExp .= '^\[\[(?<label>.*)\]\] ?(?<title>.*)$'; 
64 1
        $regExp .= '/U'; // Ungreedy
65
66 1
        $item = $this->item;
67
        
68 1
        $item['title'] = preg_replace_callback(
69 1
            $regExp,
70 1
            function ($matches) use (&$item) {
71
                
72
                // the new item label
73 1
                $item['label'] = $matches['label'];
74
                
75
                // the toc
76 1
                $item['toc'][0]['label'] = $matches['label'];
77 1
                $item['toc'][0]['title'] = $matches['title'];
78
                
79
                // the new title
80 1
                return $matches['title'];
81 1
            },
82 1
            $item['title']
83 1
        );
84
        
85 1
        $this->item = $item;
86 1
    }
87
}
88