Passed
Pull Request — master (#4)
by Warwick
02:43
created

Button::get_link()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 13
rs 9.2222
1
<?php
2
/**
3
 * LSX_Sharing_Button
4
 *
5
 * @package lsx-sharing
6
 */
7
8
namespace lsx\sharing\classes\frontend;
9
10
/**
11
 * LSX Sharing buttons class.
12
 *
13
 * @package lsx-sharing
14
 */
15
class Button {
16
17
	/**
18
	 * Services available.
19
	 *
20
	 * @var array
21
	 */
22
	public $services = array(
23
		'facebook',
24
		'twitter',
25
		'pinterest',
26
	);
27
28
	/**
29
	 * Current service.
30
	 *
31
	 * @var string
32
	 */
33
	public $service = '';
34
35
	/**
36
	 * Constructor.
37
	 */
38
	public function __construct( $service, $options, $prefix = 'sharing' ) {
39
		$this->options = $options;
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
		if ( ! in_array( $service, $this->services, true ) ) {
41
			return;
42
		}
43
		if ( \lsx\sharing\includes\functions\is_button_disabled( 'global', $service ) || \lsx\sharing\includes\functions\is_button_disabled( $prefix, $service ) ) {
44
			return '';
45
		}
46
		$this->service = $service;
47
	}
48
49
	/**
50
	 * Get service link to share.
51
	 */
52
	public function get_link( $post ) {
53
		if ( empty( $post ) ) {
54
			return '';
55
		}
56
57
		if ( 'email' === $this->service ) {
58
			return $this->get_link_email( $post );
0 ignored issues
show
Unused Code introduced by
The call to lsx\sharing\classes\fron...utton::get_link_email() has too many arguments starting with $post. ( Ignorable by Annotation )

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

58
			return $this->/** @scrutinizer ignore-call */ get_link_email( $post );

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...
Bug introduced by
Are you sure the usage of $this->get_link_email($post) targeting lsx\sharing\classes\fron...utton::get_link_email() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
59
		} elseif ( 'facebook' === $this->service ) {
60
			return $this->get_link_facebook( $post );
61
		} elseif ( 'twitter' === $this->service ) {
62
			return $this->get_link_twitter( $post );
63
		} elseif ( 'pinterest' === $this->service ) {
64
			return $this->get_link_pinterest( $post );
65
		}
66
	}
67
68
	/**
69
	 * Get Facebook link to share.
70
	 */
71
	public function get_link_facebook( $post ) {
72
		$permalink = get_permalink( $post->ID );
0 ignored issues
show
Bug introduced by
The function get_permalink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
		$permalink = /** @scrutinizer ignore-call */ get_permalink( $post->ID );
Loading history...
73
		$permalink = apply_filters( 'lsx_sharing_facebook_url', $permalink, $post );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
		$permalink = /** @scrutinizer ignore-call */ apply_filters( 'lsx_sharing_facebook_url', $permalink, $post );
Loading history...
74
		$title     = apply_filters( 'the_title', $post->post_title );
75
76
		return 'https://www.facebook.com/sharer.php?display=page&u=' . rawurlencode( $permalink ) . '&t=' . rawurlencode( $title );
77
	}
78
79
	/**
80
	 * Get Twitter link to share.
81
	 */
82
	public function get_link_twitter( $post ) {
83
		$permalink = get_permalink( $post->ID );
0 ignored issues
show
Bug introduced by
The function get_permalink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
		$permalink = /** @scrutinizer ignore-call */ get_permalink( $post->ID );
Loading history...
84
		$permalink = apply_filters( 'lsx_sharing_twitter_url', $permalink, $post );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

84
		$permalink = /** @scrutinizer ignore-call */ apply_filters( 'lsx_sharing_twitter_url', $permalink, $post );
Loading history...
85
		$title     = apply_filters( 'the_title', $post->post_title );
86
87
		if ( function_exists( 'mb_stripos' ) ) {
88
			$strlen = 'mb_strlen';
89
			$substr = 'mb_substr';
90
		} else {
91
			$strlen = 'strlen';
92
			$substr = 'substr';
93
		}
94
95
		$short_url_length = 24;
96
97
		if ( ( $strlen( $title ) + $short_url_length ) > 140 ) {
98
			$text = $substr( $title, 0, ( 140 - $short_url_length - 1 ) ) . "\xE2\x80\xA6";
99
		} else {
100
			$text = $title;
101
		}
102
103
		return 'https://twitter.com/intent/tweet?text=' . rawurlencode( $text ) . '&url=' . rawurlencode( $permalink );
104
	}
105
106
	/**
107
	 * Get Pinterest link to share.
108
	 */
109
	public function get_link_pinterest( $post ) {
110
		$permalink = get_permalink( $post->ID );
0 ignored issues
show
Bug introduced by
The function get_permalink was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

110
		$permalink = /** @scrutinizer ignore-call */ get_permalink( $post->ID );
Loading history...
111
		$permalink = apply_filters( 'lsx_sharing_pinterest_url', $permalink, $post );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

111
		$permalink = /** @scrutinizer ignore-call */ apply_filters( 'lsx_sharing_pinterest_url', $permalink, $post );
Loading history...
112
		$title     = apply_filters( 'the_title', $post->post_title );
113
114
		if ( ! has_post_thumbnail( $post ) ) {
0 ignored issues
show
Bug introduced by
The function has_post_thumbnail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

114
		if ( ! /** @scrutinizer ignore-call */ has_post_thumbnail( $post ) ) {
Loading history...
115
			if ( class_exists( 'lsx\legacy\Placeholders' ) ) {
116
				$image = lsx\legacy\Placeholders::placeholder_url( null, $post->post_type );
0 ignored issues
show
Bug introduced by
The type lsx\sharing\classes\fron...lsx\legacy\Placeholders 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...
117
			} elseif ( class_exists( 'LSX_Placeholders' ) ) {
118
				$image = LSX_Placeholders::placeholder_url( null, $post->post_type );
0 ignored issues
show
Bug introduced by
The type lsx\sharing\classes\frontend\LSX_Placeholders 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...
119
			}
120
		} else {
121
			$image = get_the_post_thumbnail_url( $post->ID, 'large' );
0 ignored issues
show
Bug introduced by
The function get_the_post_thumbnail_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

121
			$image = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url( $post->ID, 'large' );
Loading history...
122
		}
123
124
		return 'https://www.pinterest.com/pin/create/button/?url=' . rawurlencode( $permalink ) . '&media=' . rawurlencode( $image ) . '&description=' . rawurlencode( $title );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
	}
126
127
	/**
128
	 * Get Email Link.
129
	 */
130
	public function get_link_email() {
131
	}
132
}
133