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 |
||
| 23 | class Client extends BaseClient |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $baseUri = 'https://api.weixin.qq.com/cgi-bin/'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Allow media type. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $allowTypes = ['image', 'voice', 'video', 'thumb']; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Upload image. |
||
| 39 | * |
||
| 40 | * @param $path |
||
| 41 | * |
||
| 42 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 43 | * |
||
| 44 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 45 | */ |
||
| 46 | public function uploadImage($path) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Upload video. |
||
| 53 | * |
||
| 54 | * @param $path |
||
| 55 | * |
||
| 56 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 57 | * |
||
| 58 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 59 | */ |
||
| 60 | public function uploadVideo($path) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $path |
||
| 67 | * |
||
| 68 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 69 | * |
||
| 70 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 71 | */ |
||
| 72 | public function uploadVoice($path) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $path |
||
| 79 | * |
||
| 80 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 81 | * |
||
| 82 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 83 | */ |
||
| 84 | public function uploadThumb($path) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Upload temporary material. |
||
| 91 | * |
||
| 92 | * @param string $type |
||
| 93 | * @param string $path |
||
| 94 | * |
||
| 95 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 96 | * |
||
| 97 | * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
||
| 98 | */ |
||
| 99 | public function upload(string $type, string $path) |
||
| 100 | { |
||
| 101 | if (!file_exists($path) || !is_readable($path)) { |
||
| 102 | throw new InvalidArgumentException(sprintf("File does not exist, or the file is unreadable: '%s'", $path)); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!in_array($type, $this->allowTypes, true)) { |
||
| 106 | throw new InvalidArgumentException(sprintf("Unsupported media type: '%s'", $type)); |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this->httpUpload('media/upload', ['media' => $path], ['type' => $type]); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $path |
||
| 114 | * @param string $title |
||
| 115 | * @param string $description |
||
| 116 | * |
||
| 117 | * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string |
||
| 118 | */ |
||
| 119 | public function uploadVideoForBroadcasting(string $path, string $title, string $description) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $mediaId |
||
| 133 | * @param string $title |
||
| 134 | * @param string $description |
||
| 135 | * |
||
| 136 | * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 137 | */ |
||
| 138 | public function createVideoForBroadcasting(string $mediaId, string $title, string $description) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Fetch item from WeChat server. |
||
| 149 | * |
||
| 150 | * @param string $mediaId |
||
| 151 | * |
||
| 152 | * @return \EasyWeChat\Kernel\Http\StreamResponse|\Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function get(string $mediaId) |
|
| 168 | } |
||
| 169 |