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

ConvertEventSubscriber::beforeConvert()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
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