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

ImageNode::render()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 8
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace dokuwiki\plugin\prosemirror\parser;
4
5
class ImageNode extends Node implements InlineNodeInterface
6
{
7
8
    /** @var  Node */
9
    protected $parent;
10
11
    protected $attrs = [];
12
13
    protected $textNode = null;
14
15
    public function __construct($data, $parent, $previousNode = false)
16
    {
17
        $this->parent = &$parent;
18
        $this->attrs = $data['attrs'];
19
20
        // every inline node needs a TextNode to track marks
21
        $this->textNode = new TextNode(['marks' => $data['marks']], $parent, $previousNode);
22
    }
23
24
    public function toSyntax()
25
    {
26
        $title = '';
27
        if ($this->attrs['title']) {
28
            $title = '|' . $this->attrs['title'];
29
        }
30
31
        $leftAlign = '';
32
        $rightAlign = '';
33
        if ($this->attrs['align'] === 'left') {
34
            $rightAlign = ' ';
35
        } elseif ($this->attrs['align'] === 'right') {
36
            $leftAlign = ' ';
37
        } elseif ($this->attrs['align'] === 'center') {
38
            $leftAlign = ' ';
39
            $rightAlign = ' ';
40
        }
41
42
        $query = [];
43
        if ($this->attrs['height']) {
44
            $query[] = $this->attrs['width'] . 'x' . $this->attrs['height'];
45
        } elseif ($this->attrs['width']) {
46
            $query[] = $this->attrs['width'];
47
        }
48
        if ($this->attrs['linking'] && $this->attrs['linking'] !== 'details') {
49
            $query[] = $this->attrs['linking'];
50
        }
51
        if ($this->attrs['cache'] && $this->attrs['cache'] !== 'cache') {
52
            $query[] = $this->attrs['cache'];
53
        }
54
55
        $queryString = '';
56
        if (!empty($query)) {
57
            $queryString = '?' . implode('&', $query);
58
        }
59
60
61
        return '{{' . $leftAlign . $this->attrs['id'] . $queryString . $rightAlign . $title . '}}';
62
    }
63
64
    public static function render(
65
        \renderer_plugin_prosemirror $renderer,
66
        $src,
67
        $title = null,
68
        $align = null,
69
        $width = null,
70
        $height = null,
71
        $cache = null,
72
        $linking = null
73
    ) {
74
        $node = new \dokuwiki\plugin\prosemirror\schema\Node('image');
75
76
        self::addAttributes(
77
            $node,
78
            $src,
79
            $title,
80
            $align,
81
            $width,
82
            $height,
83
            $cache,
84
            $linking
85
        );
86
87
        foreach (array_keys($renderer->getCurrentMarks()) as $mark) {
88
            $node->addMark(new \dokuwiki\plugin\prosemirror\schema\Mark($mark));
89
        }
90
91
        global $ID;
92
        $node->attr('data-resolvedHtml',
93
            self::resolveMedia($src, $title, $align, $width, $height, $cache, $linking));
94
95
        $renderer->addToNodestack($node);
96
    }
97
98
    public static function addAttributes(
99
        \dokuwiki\plugin\prosemirror\schema\Node $node,
100
        $src,
101
        $title = null,
102
        $align = null,
103
        $width = null,
104
        $height = null,
105
        $cache = null,
106
        $linking = null,
107
        $prefix = ''
108
    ) {
109
        $node->attr($prefix . 'src', ml($src));
1 ignored issue
show
Bug introduced by
The function ml 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

109
        $node->attr($prefix . 'src', /** @scrutinizer ignore-call */ ml($src));
Loading history...
110
        $node->attr($prefix . 'title', $title);
111
112
        $class = 'media';
113
        if ($align === 'right') {
114
            $class = 'mediaright';
115
        } elseif ($align === 'left') {
116
            $class = 'medialeft';
117
        } elseif ($align === 'center') {
118
            $class = 'mediacenter';
119
        }
120
121
        if ($cache !== null && $cache === 'cache') {
122
            $cache = null;
123
        }
124
125
        $node->attr($prefix . 'class', $class);
126
        $node->attr($prefix . 'align', $align);
127
        $node->attr($prefix . 'width', $width);
128
        $node->attr($prefix . 'height', $height);
129
        $node->attr($prefix . 'id', $src);
130
        $node->attr($prefix . 'cache', $cache);
131
        $node->attr($prefix . 'linking', $linking);
132
    }
133
134
    public static function resolveMedia(
135
        $src,
136
        $title = null,
137
        $align = null,
138
        $width = null,
139
        $height = null,
140
        $cache = null,
141
        $linking = null
142
    ) {
143
        $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

143
        $xhtml_renderer = /** @scrutinizer ignore-call */ p_get_renderer('xhtml');
Loading history...
144
        if (media_isexternal($src) || link_isinterwiki($src)) {
2 ignored issues
show
Bug introduced by
The function link_isinterwiki 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

144
        if (media_isexternal($src) || /** @scrutinizer ignore-call */ link_isinterwiki($src)) {
Loading history...
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

144
        if (/** @scrutinizer ignore-call */ media_isexternal($src) || link_isinterwiki($src)) {
Loading history...
145
            $xhtml_renderer->externalmedia(
146
                $src,
147
                $title ?: $src,
148
                $align,
149
                $width,
150
                $height,
151
                $cache,
152
                $linking
153
            );
154
        } else {
155
            $xhtml_renderer->internalmedia(
156
                $src,
157
                $title ?: $src,
158
                $align,
159
                $width,
160
                $height,
161
                $cache,
162
                $linking
163
            );
164
        }
165
        return $xhtml_renderer->doc;
166
    }
167
168
    /**
169
     * @param string $markType
170
     */
171
    public function increaseMark($markType)
172
    {
173
        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...
174
    }
175
176
    public function getStartingNodeMarkScore($markType)
177
    {
178
        return $this->textNode->getStartingNodeMarkScore($markType);
179
    }
180
}
181