Passed
Pull Request — master (#904)
by Songda
02:08
created

CloseShortcut   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 19
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlugins() 0 13 4
A defaultPlugins() 0 8 1
A combinePlugins() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Shortcut\Wechat;
6
7
use Yansongda\Pay\Contract\ShortcutInterface;
8
use Yansongda\Pay\Exception\Exception;
9
use Yansongda\Pay\Exception\InvalidParamsException;
10
use Yansongda\Pay\Plugin\ParserPlugin;
11
use Yansongda\Pay\Plugin\Wechat\LaunchPlugin;
12
use Yansongda\Pay\Plugin\Wechat\Pay\Common\ClosePlugin;
13
use Yansongda\Pay\Plugin\Wechat\PreparePlugin;
14
use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin;
15
use Yansongda\Supports\Str;
16
17
class CloseShortcut implements ShortcutInterface
18
{
19
    /**
20
     * @throws InvalidParamsException
21
     */
22
    public function getPlugins(array $params): array
23
    {
24
        if (isset($params['combine_out_trade_no']) || isset($params['sub_orders'])) {
25
            return $this->combinePlugins();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->combinePlugins() returns the type array<integer,string> which is incompatible with the return type mandated by Yansongda\Pay\Contract\S...Interface::getPlugins() of Yansongda\Pay\Contract\PluginInterface[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
26
        }
27
28
        $typeMethod = Str::camel($params['_action'] ?? 'default').'Plugins';
29
30
        if (method_exists($this, $typeMethod)) {
31
            return $this->{$typeMethod}();
32
        }
33
34
        throw new InvalidParamsException(Exception::PARAMS_SHORTCUT_ACTION_INVALID, "Query action [{$typeMethod}] not supported");
35
    }
36
37
    protected function defaultPlugins(): array
38
    {
39
        return [
40
            PreparePlugin::class,
41
            ClosePlugin::class,
42
            RadarSignPlugin::class,
43
            LaunchPlugin::class,
44
            ParserPlugin::class,
45
        ];
46
    }
47
48
    protected function combinePlugins(): array
49
    {
50
        return [
51
            PreparePlugin::class,
52
            \Yansongda\Pay\Plugin\Wechat\Pay\Combine\ClosePlugin::class,
53
            RadarSignPlugin::class,
54
            LaunchPlugin::class,
55
            ParserPlugin::class,
56
        ];
57
    }
58
}
59