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.
Completed
Pull Request — master (#32)
by Tomasz
05:52
created

EventsTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 2.94 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 0 Features 4
Metric Value
wmc 11
c 4
b 0
f 4
lcom 1
cbo 9
dl 3
loc 102
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testFilterShortcodes() 0 19 1
B testRaw() 0 25 4
B testStripContentOutsideShortcodes() 0 26 4
A testDefaultApplier() 3 19 1
A testExceptionOnHandlerForUnknownEvent() 0 6 1

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
namespace Thunder\Shortcode\Tests;
3
4
use Thunder\Shortcode\Event\ApplyResultsEvent;
5
use Thunder\Shortcode\EventContainer\EventContainer;
6
use Thunder\Shortcode\Event\FilterShortcodesEvent;
7
use Thunder\Shortcode\Events;
8
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
9
use Thunder\Shortcode\Parser\RegularParser;
10
use Thunder\Shortcode\Processor\Processor;
11
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
12
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
13
14
/**
15
 * @author Tomasz Kowalczyk <[email protected]>
16
 */
17
final class EventsTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testFilterShortcodes()
20
    {
21
        $handlers = new HandlerContainer();
22
        $handlers->add('root', function(ShortcodeInterface $s) { return 'root['.$s->getContent().']'; });
23
        $handlers->add('yes', function(ShortcodeInterface $s) { return 'yes['.$s->getContent().']'; });
24
        $handlers->add('no', function(ShortcodeInterface $s) { return 'nope'; });
0 ignored issues
show
Unused Code introduced by
The parameter $s is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
26
        $events = new EventContainer();
27
        $events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) {
28
            $event->setShortcodes(array_filter($event->getShortcodes(), function(ShortcodeInterface $s) {
29
                return $s->getName() !== 'no';
30
            }));
31
        });
32
33
        $processor = new Processor(new RegularParser(), $handlers);
34
        $processor = $processor->withEventContainer($events);
35
36
        $this->assertSame('x root[ yes[ yes[] ] yes[ [no /] ] ] y', $processor->process('x [root] [yes] [yes/] [/yes] [yes] [no /] [/yes] [/root] y'));
37
    }
38
39
    public function testRaw()
40
    {
41
        $times = 0;
42
        $handlers = new HandlerContainer();
43
        $handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); });
44
        $handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); });
45
        $handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); });
46
47
        $events = new EventContainer();
48
        $events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) {
49
            if(!$event->getParent()) {
50
                return;
51
            }
52
            if($event->getParent()->getName() === 'raw' || $event->getParent()->hasAncestor('raw')) {
53
                $event->setShortcodes(array());
54
            }
55
        });
56
57
        $processor = new Processor(new RegularParser(), $handlers);
58
        $processor = $processor->withEventContainer($events);
59
60
        $this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]'));
61
        $this->assertSame('x un [n] [c]cnt[/c] [/n]  y', $processor->process('x [c]u[n][/c][raw] [n] [c]cnt[/c] [/n] [/raw] y'));
62
        $this->assertEquals(2, $times);
63
    }
64
65
    public function testStripContentOutsideShortcodes()
66
    {
67
        $handlers = new HandlerContainer();
68
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
69
        $handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
70
        $handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
71
72
        $events = new EventContainer();
73
        $events->addListener(Events::APPLY_RESULTS, function(ApplyResultsEvent $event) {
74
            if(!$event->getShortcode()) {
75
                return;
76
            }
77
            if('root' === $event->getShortcode()->getName()) {
78
                $replaces = array();
79
                foreach($event->getReplaces() as $replace) {
80
                    $replaces[] = $replace[0];
81
                }
82
                $event->setResult(implode('', $replaces));
83
            }
84
        });
85
86
        $processor = new Processor(new RegularParser(), $handlers);
87
        $processor = $processor->withEventContainer($events);
88
89
        $this->assertSame('a root[name name ] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
90
    }
91
92
    public function testDefaultApplier()
93
    {
94
        $handlers = new HandlerContainer();
95
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
96
        $handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
97
        $handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
98
99
        $events = new EventContainer();
100
        $events->addListener(Events::APPLY_RESULTS, function(ApplyResultsEvent $event) {
101 View Code Duplication
            $event->setResult(array_reduce(array_reverse($event->getReplaces()), function($state, array $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
                return mb_substr($state, 0, $item[1]).$item[0].mb_substr($state, $item[1] + $item[2]);
103
            }, $event->getText()));
104
        });
105
106
        $processor = new Processor(new RegularParser(), $handlers);
107
        $processor = $processor->withEventContainer($events);
108
109
        $this->assertSame('a root[x name c name  y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
110
    }
111
112
    public function testExceptionOnHandlerForUnknownEvent()
113
    {
114
        $events = new EventContainer();
115
        $this->setExpectedException('InvalidArgumentException');
116
        $events->addListener('invalid', function() {});
117
    }
118
}
119