Completed
Pull Request — master (#11)
by Tomáš
03:09
created

ConvertEventSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 34.48 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 20
loc 58
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A beforeConvert() 11 11 3
A afterConvert() 9 9 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
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Markdown\EventSubscriber;
13
14
use Symplify\PHP7_Sculpin\Markdown\MarkdownConverter;
15
use Symplify\PHP7_Sculpin\TwigBundle\SculpinTwigBundle;
16
use Symplify\PHP7_Sculpin\Event\ConvertEvent;
17
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
final class ConvertEventSubscriber implements EventSubscriberInterface
21
{
22
    /**
23
     * @var string[]
24
     */
25
    private $addPlaceholderRe = [
26
        '/^({%\s+block\s+(\w+).+?%})$/m',  // {% %} style code
27
        '/^({%\s+endblock\s+%})$/m',       // {% %} style code
28
        '/^({{.+?}})$/m',                  // {{ }} style code
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
    ];
30
31
    /**
32
     * Placeholder text.
33
     *
34
     * @var string
35
     */
36
    private $placeholder = "\n<div><!-- sculpin-hidden -->$1<!-- /sculpin-hidden --></div>\n";
37
38
    /**
39
     * Regex used to remove placeholder.
40
     *
41
     * @var string
42
     */
43
    private $removePlaceholderRe = "/(\n?<div><!-- sculpin-hidden -->|<!-- \/sculpin-hidden --><\/div>\n|\n?&lt;div&gt;&lt;!-- sculpin-hidden --&gt;|&lt;!-- \/sculpin-hidden --&gt;&lt;\/div&gt;\n)/m";
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public static function getSubscribedEvents() : array
49
    {
50
        return [
51
            SculpinEvents::BEFORE_CONVERT => 'beforeConvert',
52
            SculpinEvents::AFTER_CONVERT => 'afterConvert',
53
        ];
54
    }
55
56 View Code Duplication
    public function beforeConvert(ConvertEvent $convertEvent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
57
    {
58
        if ($convertEvent->isHandledBy(MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)) {
59
            $content = $convertEvent->source()->content();
60
            foreach ($this->addPlaceholderRe as $re) {
61
                $content = preg_replace($re, $this->placeholder, $content);
62
            }
63
            $content = preg_replace('/{%\s+(\w+)\s+(\w+\++\w+)\s+%}(.*){%\s+end\1\s+%}/Us', '<div data-$1="$2">$3</div>', $content);
64
            $convertEvent->source()->setContent($content);
65
        }
66
    }
67
68 View Code Duplication
    public function afterConvert(ConvertEvent $convertEvent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        if ($convertEvent->isHandledBy(MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)) {
71
            $content = $convertEvent->source()->content();
72
            $content = preg_replace($this->removePlaceholderRe, '', $content);
73
            $content = preg_replace('/<div data-(\w+)="(\w+\++\w+)">(.*?)<\/div>/Us', '{% $1 \'$2\' %}$3{% end$1 %}', $content);
74
            $convertEvent->source()->setContent($content);
75
        }
76
    }
77
}
78