Completed
Push — master ( c335f0 )
by Carlos
02:31
created

LuckMoney::sendNormal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * LuckMoney.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author bontian <[email protected]>
11
 *
12
 */
13
14
namespace Overtrue\Wechat;
15
16
use Overtrue\Wechat\Exception;
17
use Overtrue\Wechat\Payment\Business;
18
use Overtrue\Wechat\Utils\XML;
19
use Overtrue\Wechat\Utils\SignGenerator;
20
use Overtrue\Wechat\Http;
21
22
class LuckMoney
23
{
24
    /**
25
     * 发送现金红包
26
     */
27
    const API_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
28
29
    /**
30
     * 发送裂变红包
31
     */
32
    const API_GROUP_SEND = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
33
34
    /**
35
     * 红包查询
36
     */
37
    const API_QUERY = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
38
39
    /**
40
     * 红包预下单接口
41
     */
42
    const API_PREORDER = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/hbpreorder';
43
44
    const TYPE_CASH_LUCK_MONEY = 'NORMAL';   //红包类型,现金红包
45
    const TYPE_GROUP_LUCK_MONEY = 'GROUP';  //红包类型,裂变红包
46
47
48
49
    /**
50
     * 商户信息
51
     *
52
     * @var Business
53
     */
54
    protected $business;
55
56
    public function __construct(Business $business)
57
    {
58
        if (!is_null($business)) {
59
            $this->setBusiness($business);
60
        }
61
    }
62
63
    /**
64
     * 设置商户
65
     *
66
     * @param Business $business
67
     *
68
     * @return $this
69
     */
70
    public function setBusiness(Business $business)
71
    {
72
        if (!is_null($business)) {
73
            $this->business = $business;
74
        }
75
        return $this;
76
    }
77
78
    /**
79
     * 红包预下单,主要用于摇一摇红包活动
80
     *
81
     * <pre>
82
     * $data:
83
     * {
84
     *     "mch_billno": "198374613512",
85
     *     "send_name":"某某公司",
86
     *     "hb_type":"某某公司",
87
     *     "total_amount": 1000,
88
     *     "total_num": 1,
89
     *     "wishing": "祝福语",
90
     *     "act_name": "活动名称",
91
     *     "remark": "红包备注",
92
     *     "risk_cntl": "NORMAL",
93
     * }
94
     * </pre>
95
     *
96
     * @param array $data
97
     *
98
     * @return array
99
     */
100
    public function preOrder(array $data)
101
    {
102
        $defaultParam['nonce_str'] = uniqid('pre_');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaultParam was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultParam = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
103
        $defaultParam['mch_id'] = $this->business->mch_id;
0 ignored issues
show
Documentation introduced by
The property mch_id does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
        $defaultParam['wxappid'] = $this->business->appid;
0 ignored issues
show
Documentation introduced by
The property appid does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
106
        //用于发红包时微信支付识别摇周边红包,所有开发者统一填写摇周边平台的商户号:1000052601
107
        $defaultParam['auth_mchid'] = '1000052601';
108
        //用于发红包时微信支付识别摇周边红包,所有开发者统一填写摇周边平台的appid:wxbf42bd79c4391863
109
        $defaultParam['auth_appid'] = 'wxbf42bd79c4391863';
110
111
        $defaultParam['amt_type'] = 'ALL_RAND';
112
113
        $param = array_merge($data, $defaultParam);
114
        $signGenerator = new SignGenerator($param);
115
        $me = $this;
116
        $signGenerator->onSortAfter(function (SignGenerator $that) use ($me) {
117
            $that->key = $me->business->mch_key;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Overtrue\Wechat\Utils\SignGenerator>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property mch_key does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
        });
119
120
        $sign = $signGenerator->getResult();
121
        $param['sign'] = $sign;
122
123
        $request = XML::build($param);
124
125
        //设置Http使用的证书
126
        $options['sslcert_path'] = $this->business->getClientCert();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
127
        $options['sslkey_path'] = $this->business->getClientKey();
128
129
        $http = new Http();
130
131
        $response = $http->request(self::API_PREORDER, Http::POST, $request, $options);
0 ignored issues
show
Documentation introduced by
$request is of type string, but the function expects a array.

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...
132
133
        if (empty($response)) {
134
            throw new Exception('Create PreOrder failed.');
135
        }
136
137
        $result = XML::parse($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, 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...
138
139
        return $result;
140
    }
141
142
    /**
143
     * 发送红包
144
     *
145
     * <pre>
146
     * $data:
147
     * {
148
     *     "mch_billno": "198374613512",
149
     *     "send_name":"某某公司",
150
     *     "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew",
151
     *     "total_amount": 1000,
152
     *     "wishing": "祝福语",
153
     *     "act_name": "活动名称",
154
     *     "total_num": 1,
155
     *     "remark": "红包备注"
156
     * }
157
     * </pre>
158
     *
159
     * @param array $data
160
     * @param int   $type
161
     *
162
     * @return array
163
     */
164
    public function send(array $data, $type = self::TYPE_CASH_LUCK_MONEY)
0 ignored issues
show
Coding Style introduced by
send uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
165
    {
166
        $defaultParam['nonce_str'] = uniqid('pre_');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaultParam was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultParam = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
167
        $defaultParam['mch_id'] = $this->business->mch_id;
0 ignored issues
show
Documentation introduced by
The property mch_id does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
168
        $defaultParam['wxappid'] = $this->business->appid;
0 ignored issues
show
Documentation introduced by
The property appid does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
169
170
        if ($type == self::TYPE_CASH_LUCK_MONEY) {
171
            $defaultParam['client_ip'] = $_SERVER['REMOTE_ADDR'];
172
        }
173
174
        if ($type == self::TYPE_GROUP_LUCK_MONEY) {
175
            $defaultParam['amt_type'] = 'ALL_RAND';
176
        }
177
178
        $param = array_merge($data, $defaultParam);
179
        $signGenerator = new SignGenerator($param);
180
        $me = $this;
181
        $signGenerator->onSortAfter(function (SignGenerator $that) use ($me) {
182
            $that->key = $me->business->mch_key;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Overtrue\Wechat\Utils\SignGenerator>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property mch_key does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
183
        });
184
185
        $sign = $signGenerator->getResult();
186
        $param['sign'] = $sign;
187
188
        $request = XML::build($param);
189
190
        //设置Http使用的证书
191
        $options['sslcert_path'] = $this->business->getClientCert();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
192
        $options['sslkey_path'] = $this->business->getClientKey();
193
194
        $http = new Http();
195
196
        //根据红包类型决定调用的API
197
        if ($type == self::TYPE_CASH_LUCK_MONEY) {
198
            $url = self::API_SEND;
199
        } else {
200
            $url = self::API_GROUP_SEND;
201
        }
202
        $response = $http->request($url, Http::POST, $request, $options);
0 ignored issues
show
Documentation introduced by
$request is of type string, but the function expects a array.

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...
203
204
        if (empty($response)) {
205
            throw new Exception('Send LuckMoney failed.');
206
        }
207
208
        $result = XML::parse($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, 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...
209
210
        return $result;
211
    }
212
213
    /**
214
     * 发送普通红包
215
     *
216
     * <pre>
217
     * $data:
218
     * {
219
     *     "mch_billno": "198374613512",
220
     *     "send_name":"某某公司",
221
     *     "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew",
222
     *     "total_amount": 1000,
223
     *     "wishing": "祝福语",
224
     *     "act_name": "活动名称",
225
     *     "total_num": 1,
226
     *     "remark": "红包备注"
227
     * }
228
     * </pre>
229
     *
230
     * @param array $data
231
     *
232
     * @return array
233
     */
234
    public function sendNormal(array $data)
235
    {
236
        return $this->send($data, self::TYPE_CASH_LUCK_MONEY);
237
    }
238
239
    /**
240
     * 发送裂变红包
241
     *
242
     * <pre>
243
     * $data:
244
     * {
245
     *     "mch_billno": "198374613512",
246
     *     "send_name":"某某公司",
247
     *     "re_openid": "oJCvDjjQKx5LMtM_1kjK0gGQLsew",
248
     *     "total_amount": 1000,
249
     *     "wishing": "祝福语",
250
     *     "act_name": "活动名称",
251
     *     "total_num": 1,
252
     *     "remark": "红包备注"
253
     * }
254
     * </pre>
255
     *
256
     * @param array $data
257
     *
258
     * @return array
259
     */
260
    public function sendGroup(array $data)
261
    {
262
        return $this->send($data, self::TYPE_GROUP_LUCK_MONEY);
263
    }
264
265
    /**
266
     * 查询红包信息
267
     *
268
     * @param string $mchBillNumber
269
     *
270
     * @return array
271
     */
272
    public function query($mchBillNumber)
273
    {
274
        if (empty($mchBillNumber)) {
275
            throw new Exception('mch_id is required');
276
        }
277
278
        $param['mch_billno'] = $mchBillNumber;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$param was never initialized. Although not strictly required by PHP, it is generally a good practice to add $param = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
279
        $param['nonce_str'] = uniqid('pre_');
280
        $param['mch_id'] = $this->business->mch_id;
0 ignored issues
show
Documentation introduced by
The property mch_id does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
281
        $param['appid'] = $this->business->appid;
0 ignored issues
show
Documentation introduced by
The property appid does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
282
        $param['bill_type'] = 'MCHT';
283
284
        $signGenerator = new SignGenerator($param);
285
        $me = $this;
286
        $signGenerator->onSortAfter(function (SignGenerator $that) use ($me) {
287
            $that->key = $me->business->mch_key;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Overtrue\Wechat\Utils\SignGenerator>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property mch_key does not exist on object<Overtrue\Wechat\Payment\Business>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
288
        });
289
290
        $sign = $signGenerator->getResult();
291
        $param['sign'] = $sign;
292
293
        $request = XML::build($param);
294
295
        //设置Http使用的证书
296
        $options['sslcert_path'] = $this->business->getClientCert();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
297
        $options['sslkey_path'] = $this->business->getClientKey();
298
299
        $http = new Http();
300
        $response = $http->request(static::API_QUERY, Http::POST, $request, $options);
0 ignored issues
show
Documentation introduced by
$request is of type string, but the function expects a array.

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...
301
302
        if (empty($response)) {
303
            throw new Exception('Get LuckMoneyInfo failed.');
304
        }
305
306
        $result = XML::parse($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, 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...
307
308
        return $result;
309
    }
310
}
311