Issues (64)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

lib/export.php (21 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * GitHub Export Manager.
4
 *
5
 * @package WordPress_GitHub_Sync
6
 */
7
8
/**
9
 * Class WordPress_GitHub_Sync_Export
10
 */
11
class WordPress_GitHub_Sync_Export {
12
13
	/**
14
	 * Option key for export user.
15
	 */
16
	const EXPORT_USER_OPTION = '_wpghs_export_user_id';
17
18
	/**
19
	 * Application container.
20
	 *
21
	 * @var WordPress_GitHub_Sync
22
	 */
23
	protected $app;
24
25
	/**
26
	 * Initializes a new export manager.
27
	 *
28
	 * @param WordPress_GitHub_Sync $app Application container.
29
	 */
30 19
	public function __construct( WordPress_GitHub_Sync $app ) {
31 19
		$this->app = $app;
32 19
	}
33
34
	/**
35
	 * Updates all of the current posts in the database on master.
36
	 *
37
	 * @return string|WP_Error
38
	 */
39 5 View Code Duplication
	public function full() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 5
		$posts = $this->app->database()->fetch_all_supported();
41
42 5
		if ( is_wp_error( $posts ) ) {
43 1
			return $posts;
44
		}
45
46 4
		$master = $this->app->api()->fetch()->master();
47
48 4
		if ( is_wp_error( $master ) ) {
49 1
			return $master;
50
		}
51
52 3
		foreach ( $posts as $post ) {
53 3
			$master->tree()->add_post_to_tree( $post );
0 ignored issues
show
The method tree does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54 3
		}
55
56 3
		$master->set_message(
0 ignored issues
show
The method set_message does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57 3
			apply_filters(
58 3
				'wpghs_commit_msg_full',
59 3
				sprintf(
60 3
					'Full export from WordPress at %s (%s)',
61 3
					site_url(),
62 3
					get_bloginfo( 'name' )
63 3
				)
64 3
			) . $this->get_commit_msg_tag()
65 3
		);
66
67 3
		$result = $this->app->api()->persist()->commit( $master );
0 ignored issues
show
It seems like $master defined by $this->app->api()->fetch()->master() on line 46 can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Persist_Client::commit() does only seem to accept object<WordPress_GitHub_Sync_Commit>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
69 3
		if ( is_wp_error( $result ) ) {
70 1
			return $result;
71
		}
72
73 2
		return $this->update_shas( $posts );
0 ignored issues
show
It seems like $posts defined by $this->app->database()->fetch_all_supported() on line 40 can also be of type array; however, WordPress_GitHub_Sync_Export::update_shas() does only seem to accept array<integer,object<WordPress_GitHub_Sync_Post>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
	}
75
76
	/**
77
	 * Updates the provided post ID in master.
78
	 *
79
	 * @param int $post_id Post ID to update.
80
	 *
81
	 * @return string|WP_Error
82
	 */
83 5
	public function update( $post_id ) {
84 5
		$post = $this->app->database()->fetch_by_id( $post_id );
85
86 5
		if ( is_wp_error( $post ) ) {
87 1
			return $post;
88
		}
89
90 4
		if ( 'trash' === $post->status() ) {
91
			return $this->delete( $post_id );
92
		}
93
94 4
		$master = $this->app->api()->fetch()->master();
95
96 4
		if ( is_wp_error( $master ) ) {
97 1
			return $master;
98
		}
99
100 3
		$master->tree()->add_post_to_tree( $post );
0 ignored issues
show
The method tree does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
101 3
		$master->set_message(
0 ignored issues
show
The method set_message does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102 3
			apply_filters(
103 3
				'wpghs_commit_msg_single',
104 3
				sprintf(
105 3
					'Syncing %s from WordPress at %s (%s)',
106 3
					$post->github_path(),
107 3
					site_url(),
108 3
					get_bloginfo( 'name' )
109 3
				),
110
				$post
111 3
			) . $this->get_commit_msg_tag()
112 3
		);
113
114 3
		$result = $this->app->api()->persist()->commit( $master );
0 ignored issues
show
It seems like $master defined by $this->app->api()->fetch()->master() on line 94 can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Persist_Client::commit() does only seem to accept object<WordPress_GitHub_Sync_Commit>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
115
116 3
		if ( is_wp_error( $result ) ) {
117 1
			return $result;
118
		}
119
120 2
		return $this->update_shas( array( $post ) );
0 ignored issues
show
array($post) is of type array<integer,object<WP_...ss_GitHub_Sync_Post>"}>, but the function expects a array<integer,object<WordPress_GitHub_Sync_Post>>.

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...
121
	}
122
123
	/**
124
	 * Updates GitHub-created posts with latest WordPress data.
125
	 *
126
	 * @param array<WordPress_GitHub_Sync_Post> $posts Array of Posts to create.
127
	 *
128
	 * @return string|WP_Error
129
	 */
130 4
	public function new_posts( array $posts ) {
131 4
		$master = $this->app->api()->fetch()->master();
132
133 4
		if ( is_wp_error( $master ) ) {
134 1
			return $master;
135
		}
136
137 3
		foreach ( $posts as $post ) {
138 3
			$master->tree()->add_post_to_tree( $post );
0 ignored issues
show
The method tree does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
139 3
		}
140
141 3
		$master->set_message(
0 ignored issues
show
The method set_message does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142 3
			apply_filters(
143 3
				'wpghs_commit_msg_new_posts',
144 3
				sprintf(
145 3
					'Updating new posts from WordPress at %s (%s)',
146 3
					site_url(),
147 3
					get_bloginfo( 'name' )
148 3
				)
149 3
			) . $this->get_commit_msg_tag()
150 3
		);
151
152 3
		$result = $this->app->api()->persist()->commit( $master );
0 ignored issues
show
It seems like $master defined by $this->app->api()->fetch()->master() on line 131 can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Persist_Client::commit() does only seem to accept object<WordPress_GitHub_Sync_Commit>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
153
154 3
		if ( is_wp_error( $result ) ) {
155 1
			return $result;
156
		}
157
158 2
		return $this->update_shas( $posts );
159
	}
160
161
	/**
162
	 * Deletes a provided post ID from master.
163
	 *
164
	 * @param int $post_id Post ID to delete.
165
	 *
166
	 * @return string|WP_Error
167
	 */
168 4 View Code Duplication
	public function delete( $post_id ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169 4
		$post = $this->app->database()->fetch_by_id( $post_id );
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->app->database()->fetch_by_id($post_id); of type WP_Error|WordPress_GitHub_Sync_Post adds the type WordPress_GitHub_Sync_Post to the return on line 172 which is incompatible with the return type documented by WordPress_GitHub_Sync_Export::delete of type string|WP_Error.
Loading history...
170
171 4
		if ( is_wp_error( $post ) ) {
172 1
			return $post;
173
		}
174
175 3
		$master = $this->app->api()->fetch()->master();
176
177 3
		if ( is_wp_error( $master ) ) {
178 1
			return $master;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $master; (stdClass|WP_Error|WordPr...dPress_GitHub_Sync_Tree) is incompatible with the return type documented by WordPress_GitHub_Sync_Export::delete of type string|WP_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
179
		}
180
181 2
		$master->tree()->remove_post_from_tree( $post );
0 ignored issues
show
The method tree does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
182 2
		$master->set_message(
0 ignored issues
show
The method set_message does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
183 2
			apply_filters(
184 2
				'wpghs_commit_msg_delete',
185 2
				sprintf(
186 2
					'Deleting %s via WordPress at %s (%s)',
187 2
					$post->github_path(),
188 2
					site_url(),
189 2
					get_bloginfo( 'name' )
190 2
				),
191
				$post
192 2
			) . $this->get_commit_msg_tag()
193 2
		);
194
195 2
		$result = $this->app->api()->persist()->commit( $master );
0 ignored issues
show
It seems like $master defined by $this->app->api()->fetch()->master() on line 175 can also be of type object<WordPress_GitHub_Sync_Tree> or object<stdClass>; however, WordPress_GitHub_Sync_Persist_Client::commit() does only seem to accept object<WordPress_GitHub_Sync_Commit>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
196
197 2
		if ( is_wp_error( $result ) ) {
198 1
			return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (WP_Error|stdClass|boolean) is incompatible with the return type documented by WordPress_GitHub_Sync_Export::delete of type string|WP_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
		}
200
201 1
		return __( 'Export to GitHub completed successfully.', 'wp-github-sync' );
202
	}
203
204
	/**
205
	 * Use the new tree to save sha data
206
	 * for all the updated posts.
207
	 *
208
	 * @param WordPress_GitHub_Sync_Post[] $posts Posts to fetch updated shas for.
209
	 *
210
	 * @return string|WP_Error
211
	 */
212 6
	protected function update_shas( array $posts ) {
213 6
		$master   = $this->app->api()->fetch()->master();
214 6
		$attempts = 1;
215
216 6
		while ( is_wp_error( $master ) && $attempts < 5 ) {
217 3
			$master = $this->app->api()->fetch()->master();
218 3
			$attempts ++;
219 3
		}
220
221 6
		if ( is_wp_error( $master ) ) {
222
			// @todo throw a big warning! not having the latest shas is BAD
223
			// Solution: Show error message and link to kick off sha importing.
224 3
			return $master;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $master; (stdClass|WP_Error|WordPr...dPress_GitHub_Sync_Tree) is incompatible with the return type documented by WordPress_GitHub_Sync_Export::update_shas of type string|WP_Error.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
225
		}
226
227 3
		foreach ( $posts as $post ) {
228 3
			$blob = $master->tree()->get_blob_by_path( $post->github_path() );
0 ignored issues
show
The method tree does only exist in WordPress_GitHub_Sync_Commit, but not in WordPress_GitHub_Sync_Tree and stdClass.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
229
230 3
			if ( $blob ) {
231 3
				$this->app->database()->set_post_sha( $post, $blob->sha() );
232 3
			}
233 3
		}
234
235 3
		return __( 'Export to GitHub completed successfully.', 'wp-github-sync' );
236
	}
237
238
	/**
239
	 * Saves the export user to the database.
240
	 *
241
	 * @param int $user_id User ID to export with.
242
	 *
243
	 * @return bool
244
	 */
245 1
	public function set_user( $user_id ) {
246 1
		return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
247
	}
248
249
	/**
250
	 * Gets the commit message tag.
251
	 *
252
	 * @return string
253
	 */
254 11
	protected function get_commit_msg_tag() {
255 11
		$tag = apply_filters( 'wpghs_commit_msg_tag', 'wpghs' );
256
257 11
		if ( ! $tag ) {
258
			throw new Exception( __( 'Commit message tag not set. Filter `wpghs_commit_msg_tag` misconfigured.', 'wp-github-sync' ) );
259
		}
260
261 11
		return ' - ' . $tag;
262
	}
263
}
264