Completed
Push — test/testing-packages-soft-wor... ( cf0a87 )
by
unknown
76:36 queued 69:22
created

Soft_Wp   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 53.47 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 54
loc 101
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A build_raw_urls() 6 6 1
B get_url() 48 48 7
A store() 0 3 1
A get() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Jetpack Soft WordPress package.
4
 *
5
 * @package  automattic/jetpack-soft-wordpress
6
 */
7
8
namespace Automattic\Jetpack;
9
10
/**
11
 * Class Redirect
12
 */
13
class Soft_Wp {
14
	/**
15
	 * Constructor.
16
	 *
17
	 * Static-only class, so nothing here.
18
	 */
19
	private function __construct() {}
20
21
	/**
22
	 * Strip http:// or https:// from a url, replaces forward slash with ::,
23
	 * so we can bring them directly to their site in calypso.
24
	 *
25
	 * @param string $url the full URL.
26
	 * @return string $url without the guff
27
	 */
28 View Code Duplication
	private static function build_raw_urls( $url ) {
29
		$strip_http = '/.*?:\/\//i';
30
		$url        = preg_replace( $strip_http, '', $url );
31
		$url        = str_replace( '/', '::', $url );
32
		return $url;
33
	}
34
35
	/**
36
	 * Builds and returns an URL using the jetpack.com/redirect service
37
	 *
38
	 * If $source is a simple slug, it will be sent using the source query parameter. e.g. jetpack.com/redirect?source=slug
39
	 *
40
	 * If $source is a full URL, starting with https://, it will be sent using the url query parameter. e.g. jetpack.com/redirect?url=https://wordpress.com
41
	 *
42
	 * Note: if using full URL, query parameters and anchor must be passed in $args. Any querystring of url fragment in the URL will be discarded.
43
	 *
44
	 * @param string       $source The URL handler registered in the server or the full destination URL (starting with https://).
45
	 * @param array|string $args {
46
	 *    Optional. Additional arguments to build the url.
47
	 *
48
	 *    @type string $site URL of the site; Default is current site.
49
	 *    @type string $path Additional path to be appended to the URL.
50
	 *    @type string $query Query parameters to be added to the URL.
51
	 *    @type string $anchor Anchor to be added to the URL.
52
	 * }
53
	 *
54
	 * @return string The built URL
55
	 */
56 View Code Duplication
	public static function get_url( $source, $args = array() ) {
57
58
		$url           = 'https://jetpack.com/redirect';
59
		$args          = wp_parse_args( $args, array( 'site' => self::build_raw_urls( get_home_url() ) ) );
0 ignored issues
show
Documentation introduced by
array('site' => self::bu...w_urls(get_home_url())) is of type array<string,string,{"site":"string"}>, but the function expects a string.

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...
60
		$accepted_args = array( 'site', 'path', 'query', 'anchor' );
61
62
		$source_key = 'source';
63
		$is_url     = false;
64
65
		if ( 0 === strpos( $source, 'https://' ) ) {
66
			$source_key = 'url';
67
			$is_url     = true;
68
			$source_url = \wp_parse_url( $source );
69
70
			// discard any query and fragments.
71
			$source = 'https://' . $source_url['host'] . ( isset( $source_url['path'] ) ? $source_url['path'] : '' );
72
		}
73
74
		$to_be_added = array(
75
			$source_key => rawurlencode( $source ),
76
		);
77
78
		foreach ( $args as $arg_name => $arg_value ) {
0 ignored issues
show
Bug introduced by
The expression $args of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
79
80
			if ( ! in_array( $arg_name, $accepted_args, true ) || empty( $arg_value ) ) {
81
				continue;
82
			}
83
84
			$to_be_added[ $arg_name ] = rawurlencode( $arg_value );
85
86
		}
87
88
		if ( ! empty( $to_be_added ) ) {
89
			$url = add_query_arg( $to_be_added, $url );
90
		}
91
92
		/**
93
		 * Filters the return of the Redirect URL.
94
		 *
95
		 * @since 8.6.0
96
		 *
97
		 * @param string  $url    The redirect URL.
98
		 * @param string  $source The $source informed to Redirect::get_url.
99
		 * @param array   $args   The arguments informed to Redirect::get_url.
100
		 * @param boolean $is_url Whether $source is a URL or not.
101
		 */
102
		return \apply_filters( 'jetpack_redirects_get_url', $url, $source, $args, $is_url );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $source.

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
103
	}
104
105
	public static function store( $name, $value ) {
106
		update_option( $name, $value );
107
	}
108
109
	public static function get( $name ) {
110
		return get_option( $name );
111
	}
112
113
}
114