Completed
Pull Request — master (#546)
by
unknown
05:32
created

Relation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 14.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 75
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bindPage() 0 9 1
A getPageByDeviceId() 0 21 4
A getDeviceByPageId() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Relation.php.
14
 *
15
 * @author    allen05ren <[email protected]>
16
 * @copyright 2016 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\ShakeAround;
22
23
use EasyWeChat\Core\AbstractAPI;
24
25
/**
26
 * Class Relation.
27
 */
28
class Relation extends AbstractAPI
29
{
30
    const API_DEVICE_BINDPAGE = 'https://api.weixin.qq.com/shakearound/device/bindpage';
31
    const API_RELATION_SEARCH = 'https://api.weixin.qq.com/shakearound/relation/search';
32
33
34
    /**
35
     * Bind pages for device.
36
     *
37
     * @param array $device_identifier
38
     * @param array $page_ids
39
     *
40
     * @return \EasyWeChat\Support\Collection
41
     */
42
    public function bindPage(array $device_identifier, array $page_ids)
43
    {
44
        $params = [
45
            'device_identifier' => $device_identifier,
46
            'page_ids' => $page_ids,
47
        ];
48
49
        return $this->parseJSON('json', [self::API_DEVICE_BINDPAGE, $params]);
50
    }
51
52
    /**
53
     * Get page_ids by device_id.
54
     *
55
     * @param array   $device_identifier
56
     * @param boolean $raw
57
     *
58
     * @return array|\EasyWeChat\Support\Collection
59
     */
60
    public function getPageByDeviceId(array $device_identifier, $raw = false)
61
    {
62
        $params = [
63
            'type' => 1,
64
            'device_identifier' => $device_identifier,
65
        ];
66
67
        $result = $this->parseJSON('json', [self::API_RELATION_SEARCH, $params]);
68
69
        if ($raw === true) {
70
            return $result;
71
        }
72
        $page_ids = array();
73
        if (!empty($result->data['relations'])) {
0 ignored issues
show
Documentation introduced by
The property data does not exist on object<EasyWeChat\Support\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
74
            foreach ($result->data['relations'] as $item) {
0 ignored issues
show
Documentation introduced by
The property data does not exist on object<EasyWeChat\Support\Collection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
75
                $page_ids[] = $item['page_id'];
76
            }
77
        }
78
79
        return $page_ids;
80
    }
81
82
    /**
83
     * Get devices by page_id.
84
     *
85
     * @param int $page_id
86
     * @param int $begin
87
     * @param int $count
88
     *
89
     * @return \EasyWeChat\Support\Collection
90
     */
91 View Code Duplication
    public function getDeviceByPageId($page_id, $begin, $count)
92
    {
93
        $params = [
94
            'type' => 2,
95
            'page_id' => intval($page_id),
96
            'begin' => intval($begin),
97
            'count' => intval($count),
98
        ];
99
100
        return $this->parseJSON('json', [self::API_RELATION_SEARCH, $params]);
101
    }
102
}
103