Completed
Push — master ( 68e15a...5a472b )
by Michael
03:04
created

LinkNode::getDefaultLinkSyntax2()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 3
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\plugin\prosemirror\parser;
4
5
abstract class LinkNode extends Node implements InlineNodeInterface
6
{
7
8
9
    /** @var  InlineNodeInterface */
10
    public $previous = null;
11
12
    /** @var  Node */
13
    protected $parent;
14
15
    /** @var TextNode */
16
    protected $textNode = null;
17
18
    protected $attrs = [];
19
20
    public function __construct($data, $parent, $previousNode = false)
21
    {
22
        $this->parent = &$parent;
23
        if ($previousNode !== false) {
24
            $this->previous = &$previousNode;
25
        }
26
27
        $this->attrs = $data['attrs'];
28
29
        // every inline node needs a TextNode to track marks
30
        $this->textNode = new TextNode(['marks' => $data['marks']], $parent, $previousNode);
31
    }
32
33
34
    /**
35
     * @param string $markType
36
     */
37
    public function increaseMark($markType)
38
    {
39
        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...
40
    }
41
42
    public function getStartingNodeMarkScore($markType)
43
    {
44
        return $this->textNode->getStartingNodeMarkScore($markType);
45
    }
46
47
    protected function getDefaultLinkSyntax($inner)
48
    {
49
        $title = '';
50
        $prefix = $this->textNode->getPrefixSyntax();
51
        $postfix = $this->textNode->getPostfixSyntax();
52
53
        if (!empty($this->attrs['data-name'])) {
54
            $title = '|' . $this->attrs['data-name'];
55
        } elseif (!empty($this->attrs['image-id'])) {
56
            $imageAttrs = [];
57
            foreach ($this->attrs as $key => $value) {
58
                @list ($keyPrefix, $attrKey) = explode('-', $key, 2);
59
                if ($keyPrefix === 'image') {
60
                    $imageAttrs[$attrKey] = $value;
61
                }
62
            }
63
            $imageNode = new ImageNode([
64
                'attrs' => $imageAttrs,
65
                'marks' => [],
66
            ], $this);
67
            $title = '|' . $imageNode->toSyntax();
68
        }
69
70
        return $prefix . '[[' . $inner . $title . ']]' . $postfix;
71
    }
72
73
    /**
74
     * @param \renderer_plugin_prosemirror $renderer
75
     * @param string                       $linktype
76
     * @param string|array                 $name
77
     * @param array                        $additionalAttributes
78
     */
79
    protected static function renderToJSON(
80
        \renderer_plugin_prosemirror $renderer,
81
        $linktype,
82
        $inner,
83
        $name,
84
        $additionalAttributes = []
85
    ) {
86
        global $ID;
87
        $isImage = is_array($name);
88
        $linkNode = new \dokuwiki\plugin\prosemirror\schema\Node('link');
89
        $linkNode->attr('data-type', $linktype);
90
        $linkNode->attr('data-inner', $inner);
91
        if ($isImage) {
92
            ImageNode::addAttributes(
93
                $linkNode,
94
                $name['src'],
95
                $name['title'],
96
                $name['align'],
97
                $name['width'],
98
                $name['height'],
99
                $name['cache'],
100
                null,
101
                'image-'
102
            );
103
            $linkNode->attr('data-resolvedImage', self::resolveImageTitle(
104
                $ID,
105
                $name['src'],
106
                $name['title'],
107
                $name['align'],
108
                $name['width'],
109
                $name['height'],
110
                $name['cache']
111
            ));
112
        } else {
113
            $linkNode->attr('data-name', $name);
114
        }
115
        foreach ($additionalAttributes as $attributeName => $attributeValue) {
116
            $linkNode->attr($attributeName, $attributeValue);
117
        }
118
        foreach (array_keys($renderer->getCurrentMarks()) as $mark) {
119
            $linkNode->addMark(new \dokuwiki\plugin\prosemirror\schema\Mark($mark));
120
        }
121
        $renderer->addToNodestack($linkNode);
122
    }
123
124
    public static function resolveImageTitle($pageId, $imageId, $title = null, $align = null, $width = null,
125
        $height = null, $cache = null)
126
    {
127
        /** @var \Doku_Renderer_xhtml $xhtml_renderer */
128
        $xhtml_renderer = p_get_renderer('xhtml');
1 ignored issue
show
Bug introduced by
The function p_get_renderer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

128
        $xhtml_renderer = /** @scrutinizer ignore-call */ p_get_renderer('xhtml');
Loading history...
129
        $src = $imageId;
130
        if (!media_isexternal($src)) {
1 ignored issue
show
Bug introduced by
The function media_isexternal was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        if (!/** @scrutinizer ignore-call */ media_isexternal($src)) {
Loading history...
131
            resolve_mediaid(getNS($pageId), $src, $exists);
2 ignored issues
show
Bug introduced by
The function getNS was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
            resolve_mediaid(/** @scrutinizer ignore-call */ getNS($pageId), $src, $exists);
Loading history...
Comprehensibility Best Practice introduced by
The variable $exists seems to be never defined.
Loading history...
Bug introduced by
The function resolve_mediaid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
            /** @scrutinizer ignore-call */ 
132
            resolve_mediaid(getNS($pageId), $src, $exists);
Loading history...
132
        }
133
        return $xhtml_renderer->_media(
134
            $src,
135
            $title ?: $imageId,
136
            $align,
137
            $width,
138
            $height,
139
            $cache
140
        );
141
    }
142
}
143