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.

ImageExtraPlugin   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 193
Duplicated Lines 23.32 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 45
loc 193
ccs 91
cts 91
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 8 8 1
A onItemPreParse() 10 10 1
A onItemPostParse() 8 8 1
A onItemPostDecorate() 8 8 1
B preProcessImages() 0 53 5
A processImages() 0 28 1
B internalProcessExtraImage() 11 37 6
A internalRenderArguments() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\BaseEvent;
13
use Easybook\Events\EasybookEvents;
14
use Easybook\Events\ParseEvent;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Trefoil\Util\Toolkit;
17
use Trefoil\Plugins\BasePlugin;
18
/**
19
 * This plugin brings support to extended image syntax:
20
 *
21
 * ![caption](image.name?class="myclass"&style="any_css_style_specification")
22
 *
23
 * Example:
24
 *
25
 * ![this is an image](image.jpg?class="my-image-class"&style="border:_1px_solid_blue;_padding:_10px;")
26
 *
27
 */
28
class ImageExtraPlugin extends BasePlugin implements EventSubscriberInterface
29
{
30
    /**
31
     * String to replace spaces into images specifications
32
     *
33
     * @var string
34
     */
35
    const SPACE_REPLACEMENT = '¬|{^';
36
37 1 View Code Duplication
    public static function getSubscribedEvents()
38
    {
39
        return array(
40 1
            EasybookEvents::PRE_PARSE  => array('onItemPreParse', -100), // after TwigExtensionPlugin
41 1
            EasybookEvents::POST_PARSE => 'onItemPostParse',    // after content has been parsed
42 1
            EasybookEvents::POST_DECORATE => 'onItemPostDecorate' // after templates have been rendered
43 1
        );
44
    }
45
46 1 View Code Duplication
    public function onItemPreParse(ParseEvent $event)
47
    {
48 1
        $this->init($event);
49
50 1
        $content = $event->getItemProperty('original');
51
52 1
        $content = $this->preProcessImages($content);
53
54 1
        $event->setItemProperty('original', $content);
55 1
    }
56
57 1 View Code Duplication
    public function onItemPostParse(BaseEvent $event)
58
    {
59 1
        $this->init($event);
60
61 1
        $this->item['content'] = $this->processImages($this->item['content']);
62
63 1
        $event->setItem($this->item);
64 1
    }
65
66 1 View Code Duplication
    public function onItemPostDecorate(BaseEvent $event)
67
    {
68 1
        $this->init($event);
69
70 1
        $this->item['content'] = $this->processImages($this->item['content']);
71
72 1
        $event->setItem($this->item);
73 1
    }
74
75 1
    public function preProcessImages($content)
76
    {
77 1
        $regExp = '/';
78 1
        $regExp .= '!\[(?<alt>.*)\]'; // the optional alt text
79 1
        $regExp .= '\((?<image>.*)\)'; // the image specification
80 1
        $regExp .= '/Ums'; // Ungreedy, multiline, dotall
81
82
        // PHP 5.3 compat
83 1
        $me = $this;
84
85 1
        $content = preg_replace_callback(
86 1
            $regExp,
87
            function ($matches) use ($me) {
88 1
                $image = $matches['image'];
89 1
                $arguments = '';
90
91
                // get the query
92 1
                $parts = explode('?', html_entity_decode($matches['image']));
93 1
                if (isset($parts[1])) {
94
                    // no query, nothing to do
95
96
                    // the real image
97 1
                    $image = $parts[0];
98
99
                    // allow images to include 'images/' as path, for compatibility
100
                    // with Markdown editors like MdCharm
101 1
                    $image = str_replace('images/', '', $image);
102
103
                    // get the arguments
104 1
                    parse_str($parts[1], $args);
105 1
                    $args = str_replace('"', '', $args);
106
107
                    /* replace all spaces for this to work
108
                     * (this is because of the way Markdown parses the image specification)
109
                     */
110 1
                    if (isset($args['class'])) {
111 1
                        $args['class'] = str_replace(' ', $me::SPACE_REPLACEMENT, $args['class']);
112 1
                    }
113
114 1
                    if (isset($args['style'])) {
115 1
                        $args['style'] = str_replace(' ', $me::SPACE_REPLACEMENT, $args['style']);
116 1
                    }
117
118 1
                    $arguments = $me->internalRenderArguments($args);
119 1
                }
120
121 1
                return sprintf('![%s](%s%s)', $matches['alt'], $image, ($arguments ? '?' . $arguments : ''));
122 1
            },
123
            $content
124 1
        );
125
126 1
        return $content;
127
    }
128
129 1
    public function processImages($content)
130
    {
131 1
        $regExp = '/';
132 1
        $regExp .= '<img +(?<image>.*)>';
133 1
        $regExp .= '/Ums'; // Ungreedy, multiline, dotall
134
135
        // PHP 5.3 compat
136 1
        $me = $this;
137
138 1
        $content = preg_replace_callback(
139 1
            $regExp,
140 1
            function ($matches) use ($me) {
141 1
                $image = Toolkit::parseHTMLAttributes($matches['image']);
142 1
                $image = $me->internalProcessExtraImage($image);
143 1
                $html = Toolkit::renderHTMLTag('img', null, $image);
144
145 1
                return $html;
146 1
            },
147
            $content
148 1
        );
149
150
        // ensure there is no space replacements left (it can happen if
151
        // some of the image tags were not rendered because they were
152
        // into '<pre>' or '<code>' tags)
153 1
        $content = str_replace(self::SPACE_REPLACEMENT, ' ', $content);
154
155 1
        return $content;
156
    }
157
158
    /**
159
     * @param array $image
160
     *
161
     * @return array
162
     *
163
     * @internal Should be protected but made public for PHP 5.3 compat
164
     */
165 1
    public function internalProcessExtraImage(array $image)
166
    {
167
        // replace typographic quotes (just in case, may be set by SmartyPants)
168 1
        $src = str_replace(array('&#8221;', '&#8217;'), '"', $image['src']);
169
170
        // get the query
171 1
        $parts = explode('?', html_entity_decode($src));
172 1
        if (!isset($parts[1])) {
173
            // no query, nothing to do
174 1
            return $image;
175
        }
176
177
        // the real image
178 1
        $image['src'] = $parts[0];
179
180
        // get the arguments
181 1
        parse_str($parts[1], $args);
182 1
        $args = str_replace('"', '', $args);
183
184
        // assign them
185 1 View Code Duplication
        if (isset($args['class'])) {
186 1
            $args['class'] = str_replace(self::SPACE_REPLACEMENT, ' ', $args['class']);
187 1
            $image['class'] = isset($image['class']) ? $image['class'] . ' ' . $args['class'] : $args['class'];
188 1
            unset($args['class']);
189 1
        }
190
191 1 View Code Duplication
        if (isset($args['style'])) {
192
            // replace back all spaces
193 1
            $args['style'] = str_replace(self::SPACE_REPLACEMENT, ' ', $args['style']);
194 1
            $image['style'] = isset($image['style']) ? $image['style'] . ';' . $args['style'] : $args['style'];
195 1
            unset($args['style']);
196 1
        }
197
198 1
        $image = array_merge($image, $args);
199
200 1
        return $image;
201
    }
202
203
    /**
204
     * @param array $arguments
205
     *
206
     * @return string
207
     *
208
     * @internal Should be protected but made public for PHP 5.3 compat
209
     */
210 1
    public function internalRenderArguments(array $arguments)
211
    {
212 1
        $argArray = array();
213
214 1
        foreach ($arguments as $name => $value) {
215 1
            $argArray[] = sprintf('%s="%s"', $name, $value);
216 1
        }
217
218 1
        return implode('&', $argArray);
219
    }
220
}
221