LinkNode   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 74
c 2
b 0
f 1
dl 0
loc 143
rs 10
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A increaseMark() 0 3 1
A getStartingNodeMarkScore() 0 3 1
A __construct() 0 11 2
A getDefaultLinkSyntax() 0 24 5
A resolveImageTitle() 0 26 4
A renderToJSON() 0 43 4
1
<?php
2
3
namespace dokuwiki\plugin\prosemirror\parser;
4
5
use dokuwiki\plugin\prosemirror\schema\Mark;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, dokuwiki\plugin\prosemirror\parser\Mark. 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
use dokuwiki\File\MediaResolver;
7
8
abstract class LinkNode extends Node implements InlineNodeInterface
9
{
10
    /** @var  InlineNodeInterface */
11
    public $previous;
12
13
    /** @var  Node */
14
    protected $parent;
15
16
    /** @var TextNode */
17
    protected $textNode;
18
19
    protected $attrs = [];
20
21
    public function __construct($data, Node $parent, Node $previousNode = null)
22
    {
23
        $this->parent = &$parent;
24
        if ($previousNode !== false) {
0 ignored issues
show
introduced by
The condition $previousNode !== false is always true.
Loading history...
25
            $this->previous = &$previousNode;
26
        }
27
28
        $this->attrs = $data['attrs'];
29
30
        // every inline node needs a TextNode to track marks
31
        $this->textNode = new TextNode(['marks' => $data['marks'] ?? null], $parent, $previousNode);
32
    }
33
34
35
    /**
36
     * @param string $markType
37
     */
38
    public function increaseMark($markType)
39
    {
40
        return $this->textNode->increaseMark($markType);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->textNode->increaseMark($markType) targeting dokuwiki\plugin\prosemir...extNode::increaseMark() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
41
    }
42
43
    public function getStartingNodeMarkScore($markType)
44
    {
45
        return $this->textNode->getStartingNodeMarkScore($markType);
46
    }
47
48
    protected function getDefaultLinkSyntax($inner)
49
    {
50
        $title = '';
51
        $prefix = $this->textNode->getPrefixSyntax();
52
        $postfix = $this->textNode->getPostfixSyntax();
53
54
        if (!empty($this->attrs['data-name'])) {
55
            $title = '|' . $this->attrs['data-name'];
56
        } elseif (!empty($this->attrs['image-id'])) {
57
            $imageAttrs = [];
58
            foreach ($this->attrs as $key => $value) {
59
                @[$keyPrefix, $attrKey] = explode('-', $key, 2);
60
                if ($keyPrefix === 'image') {
61
                    $imageAttrs[$attrKey] = $value;
62
                }
63
            }
64
            $imageNode = new ImageNode([
65
                'attrs' => $imageAttrs,
66
                'marks' => [],
67
            ], $this);
68
            $title = '|' . $imageNode->toSyntax();
69
        }
70
71
        return $prefix . '[[' . $inner . $title . ']]' . $postfix;
72
    }
73
74
    /**
75
     * @param \renderer_plugin_prosemirror $renderer
76
     * @param string                       $linktype
77
     * @param string|array                 $name
78
     * @param array                        $additionalAttributes
79
     */
80
    protected static function renderToJSON(
81
        \renderer_plugin_prosemirror $renderer,
82
        $linktype,
83
        $inner,
84
        $name,
85
        $additionalAttributes = []
86
    ) {
87
        global $ID;
88
        $isImage = is_array($name);
89
        $linkNode = new \dokuwiki\plugin\prosemirror\schema\Node('link');
90
        $linkNode->attr('data-type', $linktype);
91
        $linkNode->attr('data-inner', $inner);
92
        if ($isImage) {
93
            ImageNode::addAttributes(
94
                $linkNode,
95
                $name['src'],
96
                $name['title'],
97
                $name['align'],
98
                $name['width'],
99
                $name['height'],
100
                $name['cache'],
101
                null,
102
                'image-'
103
            );
104
            $linkNode->attr('data-resolvedImage', self::resolveImageTitle(
105
                $ID,
106
                $name['src'],
107
                $name['title'],
108
                $name['align'],
109
                $name['width'],
110
                $name['height'],
111
                $name['cache']
112
            ));
113
        } else {
114
            $linkNode->attr('data-name', $name);
115
        }
116
        foreach ($additionalAttributes as $attributeName => $attributeValue) {
117
            $linkNode->attr($attributeName, $attributeValue);
118
        }
119
        foreach (array_keys($renderer->getCurrentMarks()) as $mark) {
120
            $linkNode->addMark(new Mark($mark));
121
        }
122
        $renderer->addToNodestack($linkNode);
123
    }
124
125
    public static function resolveImageTitle(
126
        $pageId,
127
        $imageId,
128
        $title = null,
129
        $align = null,
130
        $width = null,
131
        $height = null,
132
        $cache = null
133
    ) {
134
        /** @var \Doku_Renderer_xhtml $xhtml_renderer */
135
        $xhtml_renderer = p_get_renderer('xhtml');
136
        $src = $imageId;
137
        if (!media_isexternal($src)) {
138
            $resolver = new MediaResolver(getNS($pageId));
139
            $media = $resolver->resolveId($src);
140
            if (!media_exists($media)) {
141
                return '';
142
            }
143
        }
144
        return $xhtml_renderer->_media(
145
            $src,
146
            $title ?: $imageId,
147
            $align,
148
            $width,
149
            $height,
150
            $cache
151
        );
152
    }
153
}
154