Completed
Push — master ( be02a5...a1ec95 )
by Nick
22:04 queued 02:39
created

Inspector   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inspect() 0 9 1
A findEndpoint() 0 4 1
A findDateColumns() 0 6 1
A findScopeMethods() 0 16 2
1
<?php
2
3
namespace EloquentJs\ScriptGenerator\Model;
4
5
use EloquentJs\Model\AcceptsEloquentJsQueries;
6
7
class Inspector
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $excludeScopes = ['scopeScope', 'scopeEloquentJs'];
13
14
    /**
15
     * Inspect a model class and return its metadata.
16
     *
17
     * @param AcceptsEloquentJsQueries $instance
18
     * @return Metadata
19
     */
20
    public function inspect(AcceptsEloquentJsQueries $instance)
21
    {
22
        return new Metadata(
23
            class_basename($instance),
24
            $this->findEndpoint($instance),
25
            $this->findDateColumns($instance),
26
            $this->findScopeMethods($instance)
27
        );
28
    }
29
30
    /**
31
     * Find the endpoint for this model.
32
     *
33
     * @param AcceptsEloquentJsQueries $instance
34
     * @return string
35
     */
36
    protected function findEndpoint(AcceptsEloquentJsQueries $instance)
37
    {
38
        return $instance->getEndpoint();
39
    }
40
41
    /**
42
     * Get any additional date columns for this model.
43
     *
44
     * @param AcceptsEloquentJsQueries $instance
45
     * @return array
46
     */
47
    protected function findDateColumns(AcceptsEloquentJsQueries $instance)
48
    {
49
        return array_values(
50
            array_diff($instance->getDates(), ['created_at', 'updated_at', 'deleted_at'])
0 ignored issues
show
Bug introduced by
The method getDates() does not seem to exist on object<EloquentJs\Model\AcceptsEloquentJsQueries>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        );
52
    }
53
54
    /**
55
     * Get the scope methods for this model with 'scope' prefix removed.
56
     *
57
     * @param AcceptsEloquentJsQueries $instance
58
     * @return array
59
     */
60
    protected function findScopeMethods(AcceptsEloquentJsQueries $instance)
61
    {
62
        return array_map(
63
            function($method) {
64
                return lcfirst(substr($method, 5));
65
            },
66
            array_values(
67
                array_filter(
68
                    get_class_methods($instance),
69
                    function($method) {
70
                        return substr($method, 0, 5) === 'scope' and ! in_array($method, $this->excludeScopes);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
71
                    }
72
                )
73
            )
74
        );
75
    }
76
}
77