Passed
Push — master ( 410349...4e2bae )
by litefeel
02:24
created

Writing_On_GitHub_Fetch_Client   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 30
cts 45
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 15
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A remote_contents() 0 3 1
A compare() 0 18 3
A exists() 0 7 2
B tree_recursive() 0 26 5
A blob() 0 10 2
A blob_by_path() 0 8 2
1
<?php
2
/**
3
 * Fetch API client class.
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Fetch_Client
9
 */
10
class Writing_On_GitHub_Fetch_Client extends Writing_On_GitHub_Base_Client {
11
12
    /**
13
     * Compare a commit by sha with master from the GitHub API
14
     *
15
     * @param string $sha Sha for commit to retrieve.
16
     *
17
     * @return Writing_On_GitHub_File_Info[]|WP_Error
0 ignored issues
show
Documentation introduced by
Should the return type not be stdClass|WP_Error|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
18
     */
19 1
    public function compare( $sha ) {
20
        // https://api.github.com/repos/litefeel/testwpsync/compare/861f87e8851b8debb78db548269d29f8da4d94ac...master
21 1
        $endpoint = $this->compare_endpoint();
22 1
        $branch = $this->branch();
23 1
        $data = $this->call( 'GET', "$endpoint/$sha...$branch" );
24
25 1
        if ( is_wp_error( $data ) ) {
26
            return $data;
27
        }
28
29 1
        $files = array();
30 1
        foreach ($data->files as $file) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
31 1
            $file->path = $file->filename;
32 1
            $files[] = new Writing_On_GitHub_File_Info($file);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
33 1
        }
34
35 1
        return $files;
36
    }
37
38
    /**
39
     * Calls the content API to get the post's contents and metadata
40
     *
41
     * Returns Object the response from the API
42
     *
43
     * @param Writing_On_GitHub_Post $post Post to retrieve remote contents for.
44
     *
45
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use stdClass|WP_Error.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
46
     */
47
    public function remote_contents( $post ) {
48
        return $this->call( 'GET', $this->content_endpoint( $post->github_path() ) );
0 ignored issues
show
Documentation introduced by
$post->github_path() is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
    }
50
51
52
53
    public function exists( $path ) {
54
        $result = $this->call( 'GET', $this->content_endpoint( $path ) );
55
        if ( is_wp_error( $result ) ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !is_wp_error($result);.
Loading history...
56
            return false;
57
        }
58
        return true;
59
    }
60
61
    /**
62
     * Retrieves a tree by sha recursively from the GitHub API
63
     *
64
     * @param string $sha Commit sha to retrieve tree from.
65
     *
66
     * @return Writing_On_GitHub_File_Info[]|WP_Error
0 ignored issues
show
Documentation introduced by
Should the return type not be stdClass|WP_Error|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
67
     */
68 6
    public function tree_recursive( $sha = '_default' ) {
69
70 6
        if ( '_default' === $sha ) {
71 6
            $sha = $this->branch();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $sha. This often makes code more readable.
Loading history...
72 6
        }
73
74 6
        $data = $this->call( 'GET', $this->tree_endpoint() . '/' . $sha . '?recursive=1' );
75
76 6
        if ( is_wp_error( $data ) ) {
77 4
            return $data;
78
        }
79
80 2
        $files = array();
81
82 2
        foreach ( $data->tree as $index => $thing ) {
83
            // We need to remove the trees because
84
            // the recursive tree includes both
85
            // the subtrees as well the subtrees' blobs.
86 2
            if ( 'blob' === $thing->type ) {
87 2
                $thing->status = '';
88 2
                $files[] = new Writing_On_GitHub_File_Info( $thing );
89 2
            }
90 2
        }
91
92 2
        return $files;
93
    }
94
95
    /**
96
     * Retrieves the blob data for a given sha
97
     *
98
     * @param Writing_On_GitHub_File_Info $fileinfo
99
     *
100
     * @return Writing_On_GitHub_Blob|WP_Error
0 ignored issues
show
Documentation introduced by
Should the return type not be stdClass|WP_Error|Writing_On_GitHub_Blob?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
101
     */
102 1
    public function blob( Writing_On_GitHub_File_Info $fileinfo ) {
103 1
        $data = $this->call( 'GET', $this->blob_endpoint() . '/' . $fileinfo->sha );
104
105 1
        if ( is_wp_error( $data ) ) {
106 1
            return $data;
107
        }
108
109
        $data->path = $fileinfo->path;
110
        return new Writing_On_GitHub_Blob( $data );
111
    }
112
113
    /**
114
     * Get blob by path
115
     * @param  string $path
116
     * @return Writing_On_GitHub_Blob|WP_Error
0 ignored issues
show
Documentation introduced by
Should the return type not be stdClass|WP_Error|Writing_On_GitHub_Blob?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     */
118
    public function blob_by_path( $path ) {
119
        $result = $this->call( 'GET', $this->content_endpoint( $path ) );
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
        if ( is_wp_error( $result ) ) {
121
            return $result;
122
        }
123
124
        return new Writing_On_GitHub_Blob( $result );
125
    }
126
}
127