wechatpy.enterprise.replies   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 34.78 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 47
dl 24
loc 69
rs 10
c 0
b 0
f 0
ccs 39
cts 42
cp 0.9286
wmc 9

2 Functions

Rating   Name   Duplication   Size   Complexity  
B create_reply() 24 24 8
A register_reply() 0 6 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
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3
4 10
import six
5
6 10
from wechatpy import replies
7 10
from wechatpy.fields import IntegerField
8
9 10
REPLY_TYPES = {}
10
11
12 10
def register_reply(reply_type):
13 10
    def register(cls):
14 10
        REPLY_TYPES[reply_type] = cls
15 10
        return cls
16
17 10
    return register
18
19
20 10
@register_reply('text')
21 10
class TextReply(replies.TextReply):
22 10
    agent = IntegerField('AgentID', 0)
23
24
25 10
@register_reply('image')
26 10
class ImageReply(replies.ImageReply):
27 10
    agent = IntegerField('AgentID', 0)
28
29
30 10
@register_reply('voice')
31 10
class VoiceReply(replies.VoiceReply):
32 10
    agent = IntegerField('AgentID', 0)
33
34
35 10
@register_reply('video')
36 10
class VideoReply(replies.VideoReply):
37 10
    agent = IntegerField('AgentID', 0)
38
39
40 10
@register_reply('news')
41 10
class ArticlesReply(replies.ArticlesReply):
42 10
    agent = IntegerField('AgentID', 0)
43
44
45 10 View Code Duplication
def create_reply(reply, message=None, render=False):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
46 10
    r = None
47 10
    if isinstance(reply, replies.BaseReply):
48 10
        r = reply
49 10
        if message:
50
            r.source = message.target
51
            r.target = message.source
52
            r.agent = message.agent
53 10
    elif isinstance(reply, six.string_types):
54 10
        r = TextReply(
55
            message=message,
56
            content=reply
57
        )
58 10
    elif isinstance(reply, (tuple, list)):
59 10
        if len(reply) > 10:
60 10
            raise AttributeError("Can't add more than 10 articles"
61
                                 " in an ArticlesReply")
62 10
        r = ArticlesReply(
63
            message=message,
64
            articles=reply
65
        )
66 10
    if r and render:
67 10
        return r.render()
68
    return r
69