main::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 11
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
*
4
* @package phpBB Extension - Paypal
5
* @copyright (c) 2015 tas2580 (https://tas2580.net)
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
namespace tas2580\paypal\controller;
10
11
use Symfony\Component\HttpFoundation\Response;
12
13
class main
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\db\driver\driver */
19
	protected $db;
20
21
	/** @var \phpbb\controller\helper */
22
	protected $helper;
23
24
	/** @var \phpbb\request\request */
25
	protected $request;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var \phpbb\user */
31
	protected $user;
32
33
	/**
34
	* Constructor
35
	*
36
	* @param \phpbb\config\config		$config
37
	* @param \phpbb\controller\helper	$helper
38
	* @param \phpbb\template\template	$template
39
	* @param \phpbb\user				$user
40
	*/
41
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $table_amount, $table_config, $table_donations, $table_items)
42
	{
43
		$this->config = $config;
44
		$this->db = $db;
0 ignored issues
show
Documentation Bug introduced by
It seems like $db of type object<phpbb\db\driver\driver_interface> is incompatible with the declared type object<phpbb\db\driver\driver> of property $db.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
		$this->helper = $helper;
46
		$this->request = $request;
47
		$this->template = $template;
48
		$this->user = $user;
49
		$this->table_amount = $table_amount;
0 ignored issues
show
Bug introduced by
The property table_amount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
		$this->table_config = $table_config;
0 ignored issues
show
Bug introduced by
The property table_config does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
		$this->table_donations = $table_donations;
0 ignored issues
show
Bug introduced by
The property table_donations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
		$this->table_items = $table_items;
0 ignored issues
show
Bug introduced by
The property table_items does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
	}
54
	/**
55
	* Controller for route /paypal
56
	*
57
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
58
	*/
59
	public function page()
60
	{
61
		$this->user->add_lang_ext('tas2580/paypal', 'common');
62
63
		$amount_list = '';
64
		$sql = 'SELECT *
65
			FROM ' . $this->table_amount . '
66
			ORDER BY amount_value';
67
		$result = $this->db->sql_query($sql);
68
		while ($row = $this->db->sql_fetchrow($result))
69
		{
70
			$amount_list .= '<option value="' . number_format($row['amount_value'] / 100, 2) . '">' . number_format($row['amount_value'] / 100, 2) . '</option>';
71
		}
72
73
		$sql = 'SELECT *
74
			FROM ' . $this->table_items . '
75
			ORDER BY item_name';
76
		$result = $this->db->sql_query($sql);
77
		while ($row = $this->db->sql_fetchrow($result))
78
		{
79
			$this->template->assign_block_vars('items', array(
80
				'ITEM_NAME'	=> $row['item_name'],
81
				'ITEM'		=> generate_text_for_display($row['item_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], 7),
82
				'ITEM_ID'	=> $row['item_id'],
83
			));
84
		}
85
		$sql = 'SELECT *
86
			FROM ' . $this->table_config;
0 ignored issues
show
Bug introduced by
The property table_config does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
87
		$result = $this->db->sql_query($sql);
88
		$row = $this->db->sql_fetchrow($result);
89
90
		$this->template->assign_vars(array(
91
			'PAYPAL_TITLE'				=> $row['paypal_title'],
92
			'PAYPAL_TEXT'				=> generate_text_for_display($row['paypal_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], 7),
93
			'PAYPAL_EMAIL'				=> $row['paypal_email'],
94
			'AMOUNT_LIST'				=> $amount_list,
95
			'PAYPAL_ACTION'				=> ($row['paypal_sandbox'] == 1) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr',
96
			'S_SANDBOX'					=> ($row['paypal_sandbox'] == 1) ? true : false,
97
			'S_CURL'					=> function_exists('curl_init'),
98
			'CURRENCY_CODE'				=> $this->currency_code_select($row['paypal_currency']),
99
			'CURRENCY'					=> $row['paypal_currency'],
100
			'USER_ID'					=> $this->user->data['user_id'],
101
			'IPN_URL'					=> $this->helper->route('tas2580_paypal_ipn', array(), true, '', \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL),
102
			'RETURN_URL'				=> $this->helper->route('tas2580_paypal_controller', array(), true, '', \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL),
103
		));
104
		return $this->helper->render('paypal_body.html', $row['paypal_title']);
105
	}
106
107
108
	/**
109
	 *
110
	 * https://github.com/paypal/ipn-code-samples/blob/master/paypal_ipn.php
111
	 *
112
	 * @return boolean
113
	 */
114
	public function ipn()
115
	{
116
		$raw_post_data = file_get_contents('php://input');
117
		$raw_post_array = explode('&', $raw_post_data);
118
		$myPost = array();
119
		foreach ($raw_post_array as $keyval)
120
		{
121
			$keyval = explode ('=', $keyval);
122
			if (count($keyval) == 2)
123
			{
124
				$myPost[$keyval[0]] = urldecode($keyval[1]);
125
			}
126
		}
127
		// read the post from PayPal system and add 'cmd'
128
		$req = 'cmd=_notify-validate';
129
		if (function_exists('get_magic_quotes_gpc'))
130
		{
131
			$get_magic_quotes_exists = true;
132
		}
133
		foreach ($myPost as $key => $value)
134
		{
135
			if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
0 ignored issues
show
Bug introduced by
The variable $get_magic_quotes_exists does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
136
			{
137
				$value = urlencode(stripslashes($value));
138
			}
139
			else
140
			{
141
				$value = urlencode($value);
142
			}
143
			$req .= "&$key=$value";
144
		}
145
146
		$sql = 'SELECT paypal_sandbox
147
			FROM ' . $this->table_config;
0 ignored issues
show
Bug introduced by
The property table_config does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
148
		$result = $this->db->sql_query($sql);
149
		$row = $this->db->sql_fetchrow($result);
150
151
		$paypal_url = ($row['paypal_sandbox'] == 1) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';
152
153
		$ch = curl_init($paypal_url);
154
		if ($ch == false)
155
		{
156
			return false;
157
		}
158
		curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
159
		curl_setopt($ch, CURLOPT_POST, 1);
160
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
161
		curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
162
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
163
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
164
		curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
165
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
166
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
167
		$res = curl_exec($ch);
168
		curl_close($ch);
169
170
		// Inspect IPN validation result and act accordingly
171
		// Split response headers and payload, a better way for strcmp
172
		$tokens = explode("\r\n\r\n", trim($res));
173
		$res = trim(end($tokens));
174
		if (strcmp($res, 'VERIFIED') == 0)
175
		{
176
			$sql_data = array(
177
				'user_id'			=> (int) $this->request->variable('custom', '0'),
178
				'item_id'			=> (int) $this->request->variable('item_number', '0'),
179
				'item_name'			=> $this->request->variable('item_number', '', true),
180
				'donation_time'		=> time(),
181
				'donation_amount'	=> $this->request->variable('mc_gross', '0'),
182
			);
183
184
			$sql = 'INSERT INTO ' . $this->table_donations . '
185
				' . $this->db->sql_build_array('INSERT', $sql_data);
186
			$this->db->sql_query($sql);
187
		}
188
189
		$headers = array(
190
			'Content-Type'		=> 'application/xml; charset=UTF-8',
191
		);
192
		return new Response('', '200', $headers);
193
	}
194
195
	private function currency_code_select($sel)
196
	{
197
		$codes = array(
198
			'AUD'		=> $this->user->lang('DONATION_AUD'),
199
			'CAD'		=> $this->user->lang('DONATION_CAD'),
200
			'CHF'		=> $this->user->lang('DONATION_CHF'),
201
			'CZK'		=> $this->user->lang('DONATION_CZK'),
202
			'DKK'		=> $this->user->lang('DONATION_DKK'),
203
			'EUR'		=> $this->user->lang('DONATION_EUR'),
204
			'GBP'		=> $this->user->lang('DONATION_GBP'),
205
			'HKD'		=> $this->user->lang('DONATION_HKD'),
206
			'HUF'		=> $this->user->lang('DONATION_HUF'),
207
			'ILS'		=> $this->user->lang('DONATION_ILS'),
208
			'JPY'		=> $this->user->lang('DONATION_JPY'),
209
			'MXN'		=> $this->user->lang('DONATION_MXN'),
210
			'NOK'		=> $this->user->lang('DONATION_NOK'),
211
			'NZD'		=> $this->user->lang('DONATION_NZD'),
212
			'PLN'		=> $this->user->lang('DONATION_PLN'),
213
			'SEK'		=> $this->user->lang('DONATION_SEK'),
214
			'SGD'		=> $this->user->lang('DONATION_SGD'),
215
			'USD'		=> $this->user->lang('DONATION_USD'),
216
		);
217
218
		$retrun = '';
219 View Code Duplication
		foreach ($codes as $value => $title)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
220
		{
221
			$selected = ($value == $sel) ? ' selected="selected"' : '';
222
			$retrun .= '<option value="' . $value . '"' . $selected . '>' . $title . '</option>';
223
		}
224
		return $retrun;
225
	}
226
}
227