Query::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace API\Controllers;
6
7
use API\Helpers\DataManager as DataManager;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
11
class Query
12
{
13
    public function __construct($container)
14
    {
15
        $this->container = $container;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
    }
17
18
    public function index(Request $request, Response $response, $page = 1)
19
    {
20
21
        // $query = $request->getParsedBodyParam('query', 'select * from products');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
22
        $query = $request->getParsedBodyParam('query', 'use senda');
0 ignored issues
show
Bug introduced by
The method getParsedBodyParam() does not exist on Psr\Http\Message\ServerRequestInterface. Did you maybe mean getParsedBody()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23
        $DataManager = new DataManager($query, $this->container, $page);
24
25
        if ($DataManager->isError()) {
26
            return $response->withJson($DataManager->getErrorData(), 200);
27
        }
28
29
        $data = [];
30
31
        if ($DataManager->isFetching()) {
32
            $data['fetching'] = true;
33
            $fetchedData = $DataManager->paginator->getCurrentItems();
34
            $data['count'] = $DataManager->paginator->getTotalItem();
35
            $data['currentPage'] = $DataManager->paginator->getCurrentPage();
36
            $data['totalPages'] = $DataManager->paginator->getTotalPages();
37
            $data['rowsPerPage'] = $DataManager->paginator->getRowsPerPage();
38
            $data['message'] = $data['count'].' rows has been returned.';
39
            $data['columns'] = isset($fetchedData[0]) ? array_keys($fetchedData[0]) : [];
40
            $data['rows'] = $fetchedData;
41
        } else {
42
            if ($DataManager->getCustomMessage() !== false) {
43
                $data['message'] = $DataManager->getCustomMessage();
44
            } else {
45
                $data['message'] = 'the query has been successfully performed !';
46
            }
47
        }
48
49
        // sleep(5);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
        //
51
        // $data = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
53
        return $response->withJson($data, 200);
54
    }
55
56
    public function page(Request $request, Response $response)
57
    {
58
        $query = $request->getParsedBodyParam('query', 'select * from products');
0 ignored issues
show
Bug introduced by
The method getParsedBodyParam() does not exist on Psr\Http\Message\ServerRequestInterface. Did you maybe mean getParsedBody()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
        $page = $request->getParsedBodyParam('page', 1);
0 ignored issues
show
Bug introduced by
The method getParsedBodyParam() does not exist on Psr\Http\Message\ServerRequestInterface. Did you maybe mean getParsedBody()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
61
        $db = $this->container->db;
0 ignored issues
show
Unused Code introduced by
$db is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
        $DataManager = new DataManager($query, $this->container, $page);
63
        $fetchedData = $DataManager->paginator->getCurrentItems();
64
        $data['currentPage'] = $DataManager->paginator->getCurrentPage();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
65
        $data['rows'] = $fetchedData;
66
67
        // sleep(5);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
69
        return $response->withJson($data, 200);
70
    }
71
}
72