Passed
Push — main ( c90a44...178315 )
by Nobufumi
02:25
created

printEditDataLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 14
rs 9.9
1
<?php
2
3
use Jidaikobo\Kontiki\Core\Database;
4
use Jidaikobo\Kontiki\Core\Auth;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Auth. 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...
5
use Jidaikobo\Kontiki\Models\UserModel;
6
use Jidaikobo\Kontiki\Models\PostModel;
7
use Jidaikobo\Kontiki\Services\ValidationService;
8
9
if (!function_exists('getIndex')) {
10
    /**
11
     * @param array $args Configuration for the request.
12
     * @param string $env environment.
13
     * @return array
14
     */
15
    function getIndex(array $args, string $env = 'production'): array
16
    {
17
        $app = Jidaikobo\Kontiki\Bootstrap::init($env);
18
        $model = $app->getContainer()->get(PostModel::class);
19
        $retval = [];
20
        $retval['body'] = $model->getIndexData('published', $args);
21
        $retval['pagination'] = $model->getPagination();
22
        return $retval;
23
    }
24
}
25
26
if (!function_exists('getData')) {
27
    /**
28
     * @param array $args Configuration for the request.
29
     * @param string $env environment.
30
     * @return array
31
     */
32
    function getData(array $args, string $env = 'production'): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
    {
34
        $app = Jidaikobo\Kontiki\Bootstrap::init($env);
35
        $model = $app->getContainer()->get(PostModel::class);
36
        $slug = $args['slug'] ?? '';
37
        $retval = [];
38
        $retval['body'] = $model->getByFieldWithCondtioned('slug', $slug, 'published');
39
        return $retval;
40
    }
41
}
42
43
if (!function_exists('printEditDataLink')) {
44
    /**
45
     * @param array $args Configuration for the request.
46
     * @param string $env environment.
47
     * @return void
48
     */
49
    function printEditDataLink(array $args, string $env = 'production'): void
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
    {
51
        $app = Jidaikobo\Kontiki\Bootstrap::init($env);
52
        $model = $app->getContainer()->get(PostModel::class);
53
        $slug = $args['slug'] ?? '';
54
        $retval = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $retval is dead and can be removed.
Loading history...
55
        $data = $model->getByFieldWithCondtioned('slug', $slug, 'published');
56
        $url = $app->getBasePath() . '/' . e($data['post_type']) . '/edit/' . e($data['id']);
57
58
        $html = '';
59
        $html .= '<p class="edit-this-page"><a href="' . $url . '">';
60
        $html .= __('edit_this_content');
61
        $html .= '</a></a>';
62
        echo $html;
63
    }
64
}
65