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
02:18
created

EventsTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 7.59 %

Coupling/Cohesion

Components 2
Dependencies 11

Importance

Changes 7
Bugs 0 Features 6
Metric Value
wmc 6
c 7
b 0
f 6
lcom 2
cbo 11
dl 6
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testRaw() 0 18 1
A testStripContentOutsideShortcodes() 0 15 1
A testDefaultApplier() 6 22 1
A testExceptionOnHandlerForUnknownEvent() 0 6 1
A testInvalidFilterRawShortcodesNames() 0 5 1
A testInvalidReplaceJoinNames() 0 5 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\ReplaceShortcodesEvent;
5
use Thunder\Shortcode\EventContainer\EventContainer;
6
use Thunder\Shortcode\EventHandler\FilterRawEventHandler;
7
use Thunder\Shortcode\EventHandler\ReplaceJoinEventHandler;
8
use Thunder\Shortcode\Events;
9
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
10
use Thunder\Shortcode\Parser\RegularParser;
11
use Thunder\Shortcode\Processor\Processor;
12
use Thunder\Shortcode\Shortcode\ReplacedShortcode;
13
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
14
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
15
16
/**
17
 * @author Tomasz Kowalczyk <[email protected]>
18
 */
19
final class EventsTest extends \PHPUnit_Framework_TestCase
20
{
21
    public function testRaw()
22
    {
23
        $times = 0;
24
        $handlers = new HandlerContainer();
25
        $handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); });
26
        $handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); });
27
        $handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); });
28
29
        $events = new EventContainer();
30
        $events->addListener(Events::FILTER_SHORTCODES, new FilterRawEventHandler(array('raw')));
31
32
        $processor = new Processor(new RegularParser(), $handlers);
33
        $processor = $processor->withEventContainer($events);
34
35
        $this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]'));
36
        $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'));
37
        $this->assertEquals(2, $times);
38
    }
39
40
    public function testStripContentOutsideShortcodes()
41
    {
42
        $handlers = new HandlerContainer();
43
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
44
        $handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
45
        $handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
46
47
        $events = new EventContainer();
48
        $events->addListener(Events::REPLACE_SHORTCODES, new ReplaceJoinEventHandler(array('root')));
49
50
        $processor = new Processor(new RegularParser(), $handlers);
51
        $processor = $processor->withEventContainer($events);
52
53
        $this->assertSame('a root[name name name] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[name/][/root] b'));
54
    }
55
56
    public function testDefaultApplier()
57
    {
58
        $handlers = new HandlerContainer();
59
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
60
        $handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
61
        $handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
62
63
        $events = new EventContainer();
64
        $events->addListener(Events::REPLACE_SHORTCODES, function(ReplaceShortcodesEvent $event) {
65 View Code Duplication
            $event->setResult(array_reduce(array_reverse($event->getReplacements()), function($state, ReplacedShortcode $r) {
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...
66
                $offset = $r->getOffset();
67
                $length = mb_strlen($r->getText());
68
69
                return mb_substr($state, 0, $offset).$r->getReplacement().mb_substr($state, $offset + $length);
70
            }, $event->getText()));
71
        });
72
73
        $processor = new Processor(new RegularParser(), $handlers);
74
        $processor = $processor->withEventContainer($events);
75
76
        $this->assertSame('a root[x name c name  y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
77
    }
78
79
    public function testExceptionOnHandlerForUnknownEvent()
80
    {
81
        $events = new EventContainer();
82
        $this->setExpectedException('InvalidArgumentException');
83
        $events->addListener('invalid', function() {});
84
    }
85
86
    public function testInvalidFilterRawShortcodesNames()
87
    {
88
        $this->setExpectedException('InvalidArgumentException');
89
        new FilterRawEventHandler(array(new \stdClass()));
90
    }
91
92
    public function testInvalidReplaceJoinNames()
93
    {
94
        $this->setExpectedException('InvalidArgumentException');
95
        new ReplaceJoinEventHandler(array(new \stdClass()));
96
    }
97
}
98