Completed
Push — develop ( 95b215...22eea4 )
by Sander
01:39
created

BuildsQueries::min()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sanderdekroon\Parlant\Builder;
4
5
trait BuildsQueries
6
{
7
    protected $grammar;
8
    protected $compiler;
9
    protected $bindings;
10
    protected $configuration;
11
12
    /**
13
     * Get the posts by passing the bindings, configuration and building the arguments.
14
     * @return mixed
15
     */
16 25
    public function get()
17
    {
18 25
        return $this->compiler
19 25
            ->bind($this->bindings)
20 25
            ->withConfiguration($this->configuration)
21 25
            ->build();
22
    }
23
24
    /**
25
     * Return only the first post.
26
     * @return \WP_Post
0 ignored issues
show
Bug introduced by
The type WP_Post 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...
27
     */
28 1
    public function first()
29
    {
30 1
        $this->setBinding('posts_per_page', 1);
0 ignored issues
show
Unused Code introduced by
The call to Sanderdekroon\Parlant\Bu...dsQueries::setBinding() has too many arguments starting with 'posts_per_page'. ( Ignorable by Annotation )

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

30
        $this->/** @scrutinizer ignore-call */ 
31
               setBinding('posts_per_page', 1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31 1
        $posts = $this->get();
32
33
        // If the developer has requested the argument list, we'll return the full array.
34 1
        if ($this->configuration->get('return') == 'argument') {
35 1
            return $posts;
36
        }
37
38
        // If it's an array, we'll assume it's one post wrapped within an array.
39
        // Anything else just gets returned without modification.
40
        return is_array($posts) ? reset($posts) : $posts;
41
    }
42
43
    /**
44
     * Return all posts with no limit.
45
     * @return mixed
46
     */
47 3
    public function all()
48
    {
49 3
        $this->setBinding('posts_per_page', -1);
0 ignored issues
show
Unused Code introduced by
The call to Sanderdekroon\Parlant\Bu...dsQueries::setBinding() has too many arguments starting with 'posts_per_page'. ( Ignorable by Annotation )

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

49
        $this->/** @scrutinizer ignore-call */ 
50
               setBinding('posts_per_page', -1);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50 3
        return $this->get();
51
    }
52
53
    /**
54
     * Count the total of found posts.
55
     * @return int
56
     */
57
    public function count()
58
    {
59
        $this->configuration->add('return', 'Sanderdekroon\Parlant\Formatter\CountFormatter');
60
        return $this->get();
61
    }
62
63
    /**
64
     * Pluck the values of the supplied column name
65
     * @return array
66
     */
67
    public function pluck(string $columnname)
68
    {
69
        if (!in_array($columnname, $this->grammar->getPostProperties())) {
70
            throw new \InvalidArgumentException('Invalid columnname '.$columnname);
71
        }
72
73
        $this->configuration->add('return', 'Sanderdekroon\Parlant\Formatter\ArrayFormatter');
74
75
        return array_map(function ($post) use ($columnname) {
76
            return $post->$columnname;
77
        }, $this->get());
78
    }
79
80
    /**
81
     * @todo  Implement
82
     */
83 1
    public function avg()
84
    {
85 1
        throw new \BadMethodCallException('Accessing unimplemented method.');
86
    }
87
88
    /**
89
     * @todo  Implement
90
     */
91 1
    public function max()
92
    {
93 1
        throw new \BadMethodCallException('Accessing unimplemented method.');
94
    }
95
96
    /**
97
     * @todo  Implement
98
     */
99 1
    public function min()
100
    {
101 1
        throw new \BadMethodCallException('Accessing unimplemented method.');
102
    }
103
104
105
    protected abstract function setBinding();
106
}
107