helper_plugin_prosemirror   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSyntaxFromProsemirrorData() 0 11 2
A tryToLogErrorToSentry() 0 20 2
1
<?php
2
3
use dokuwiki\Extension\Plugin;
4
use dokuwiki\plugin\prosemirror\parser\SyntaxTreeBuilder;
5
use dokuwiki\plugin\sentry\Event;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Event. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * DokuWiki Plugin prosemirror (Helper Component)
9
 *
10
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11
 * @author  Andreas Gohr <[email protected]>
12
 */
13
class helper_plugin_prosemirror extends Plugin
14
{
15
    /**
16
     * Decode json and parse the data back into DokuWiki Syntax
17
     *
18
     * @param string $unparsedJSON the json produced by Prosemirror
19
     *
20
     * @return null|string DokuWiki syntax or null on error
21
     */
22
    public function getSyntaxFromProsemirrorData($unparsedJSON)
23
    {
24
        $prosemirrorData = json_decode($unparsedJSON, true);
25
        if ($prosemirrorData === null) {
26
            $errorMsg = 'Error decoding prosemirror json ' . json_last_error_msg();
27
            throw new RuntimeException($errorMsg);
28
        }
29
30
        $rootNode = SyntaxTreeBuilder::parseDataIntoTree($prosemirrorData);
31
        $syntax = $rootNode->toSyntax();
32
        return $syntax;
33
    }
34
35
    /**
36
     * Try to log an error to sentry if the sentry plugin exists
37
     *
38
     * @param Throwable $exception
39
     * @param array     $extraData associative array for sentries `extra` field
40
     *
41
     * @return bool true if the exception has been logged to sentry, false otherwise
42
     */
43
    public function tryToLogErrorToSentry(Throwable $exception, array $extraData = [])
44
    {
45
        global $ID;
46
47
        /** @var helper_plugin_sentry $sentry */
48
        $sentry = plugin_load('helper', 'sentry');
49
        if (!$sentry) {
0 ignored issues
show
introduced by
$sentry is of type helper_plugin_sentry, thus it always evaluated to true.
Loading history...
50
            return false;
51
        }
52
        $sentryEvent = new Event([
53
            'extra' => $extraData,
54
            'tags' => [
55
                'plugin' => 'prosemirror',
56
                'id' => $ID,
57
            ],
58
        ]);
59
        $sentryEvent->addException($exception);
60
61
        $sentry->logEvent($sentryEvent);
62
        return true;
63
    }
64
}
65
// vim:ts=4:sw=4:et:
66