Passed
Pull Request — master (#2)
by Mark
02:03
created

action_plugin_description   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 30
c 1
b 0
f 0
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C description() 0 50 13
A register() 0 3 1
1
<?php
2
/*
3
 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
4
 * @noinspection AutoloadingIssuesInspection
5
 */
6
7
/**
8
 *  Description action plugin.
9
 *
10
 * @license      GPL 2 (http://www.gnu.org/licenses/gpl.html)
11
 * @author       Ikuo Obataya <[email protected]>
12
 * @author       Matthias Schulte <[email protected]>.
13
 * @author       Mark C. Prins <[email protected]>
14
 *
15
 */
16
17
use dokuwiki\Extension\ActionPlugin;
1 ignored issue
show
Bug introduced by
The type dokuwiki\Extension\ActionPlugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use dokuwiki\Extension\Event;
1 ignored issue
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...
Bug introduced by
The type dokuwiki\Extension\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use dokuwiki\Extension\EventHandler;
1 ignored issue
show
Bug introduced by
The type dokuwiki\Extension\EventHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
const KEYWORD_SOURCE_ABSTRACT = 'abstract';
22
const KEYWORD_SOURCE_GLOBAL = 'global';
23
const KEYWORD_SOURCE_SYNTAX = 'syntax';
24
25
class action_plugin_description extends ActionPlugin
26
{
27
28
    final public function register(EventHandler $controller): void
29
    {
30
        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description', array());
31
    }
32
33
    /**
34
     * Add an abstract, global value or a specified string to meta header
35
     */
36
    final public function description(Event $event, $param): void
37
    {
38
        if (empty($event->data) || empty($event->data['meta'])) {
39
            return;
40
        }
41
42
        global $ID;
43
        $source = $this->getConf('keyword_source');
44
        if (empty($source)) {
45
            $source = 'abstract';
46
        }
47
48
        if ($source === KEYWORD_SOURCE_ABSTRACT) {
49
            if (auth_quickaclcheck($ID) < AUTH_READ) {
1 ignored issue
show
Bug introduced by
The constant AUTH_READ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function auth_quickaclcheck 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

49
            if (/** @scrutinizer ignore-call */ auth_quickaclcheck($ID) < AUTH_READ) {
Loading history...
50
                // don't add meta header when user has no read permissions
51
                return;
52
            }
53
54
            $d = p_get_metadata($ID, 'description');
1 ignored issue
show
Bug introduced by
The function p_get_metadata 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

54
            $d = /** @scrutinizer ignore-call */ p_get_metadata($ID, 'description');
Loading history...
55
            if (empty($d)) {
56
                return;
57
            }
58
59
            $a = str_replace("\n", " ", $d['abstract']);
60
            if (empty($a)) {
61
                return;
62
            }
63
        }
64
65
        if ($source === KEYWORD_SOURCE_GLOBAL) {
66
            $a = $this->getConf('global_description');
67
            if (empty($a)) {
68
                return;
69
            }
70
        }
71
72
        if ($source === KEYWORD_SOURCE_SYNTAX) {
73
            if (auth_quickaclcheck($ID) < AUTH_READ) {
74
                // don't add meta header when user has no read permissions
75
                return;
76
            }
77
            $metadata = p_get_metadata($ID);
78
            $a = $metadata['plugin_description']['keywords'];
79
            if (empty($a)) {
80
                return;
81
            }
82
        }
83
84
        $m = array("name" => "description", "content" => $a);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $a does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
        $event->data['meta'][] = $m;
86
    }
87
}
88