Failed Conditions
Push — refactorSubscriptions ( 75d664...451969 )
by Michael
07:30 queued 03:08
created

PageSubscriptionSender::sendPageDiff()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 5
dl 0
loc 61
rs 8.8509
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
namespace dokuwiki\Subscriptions;
5
6
7
use Diff;
8
use InlineDiffFormatter;
9
use UnifiedDiffFormatter;
10
11
class PageSubscriptionSender extends SubscriptionSender
12
{
13
14
    /**
15
     * Send the diff for some page change
16
     *
17
     * @param string   $subscriber_mail The target mail address
18
     * @param string   $template        Mail template ('subscr_digest', 'subscr_single', 'mailtext', ...)
19
     * @param string   $id              Page for which the notification is
20
     * @param int|null $rev             Old revision if any
21
     * @param string   $summary         Change summary if any
22
     *
23
     * @return bool                     true if successfully sent
24
     */
25
    public function sendPageDiff($subscriber_mail, $template, $id, $rev = null, $summary = '')
26
    {
27
        global $DIFF_INLINESTYLES;
28
29
        // prepare replacements (keys not set in hrep will be taken from trep)
30
        $trep = [
31
            'PAGE' => $id,
32
            'NEWPAGE' => wl($id, '', true, '&'),
33
            'SUMMARY' => $summary,
34
            'SUBSCRIBE' => wl($id, ['do' => 'subscribe'], true, '&'),
35
        ];
36
        $hrep = [];
37
38
        if ($rev) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rev of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
39
            $subject = 'changed';
40
            $trep['OLDPAGE'] = wl($id, "rev=$rev", true, '&');
41
42
            $old_content = rawWiki($id, $rev);
43
            $new_content = rawWiki($id);
44
45
            $df = new Diff(
46
                explode("\n", $old_content),
47
                explode("\n", $new_content)
48
            );
49
            $dformat = new UnifiedDiffFormatter();
50
            $tdiff = $dformat->format($df);
51
52
            $DIFF_INLINESTYLES = true;
53
            $df = new Diff(
54
                explode("\n", $old_content),
55
                explode("\n", $new_content)
56
            );
57
            $dformat = new InlineDiffFormatter();
58
            $hdiff = $dformat->format($df);
59
            $hdiff = '<table>' . $hdiff . '</table>';
60
            $DIFF_INLINESTYLES = false;
61
        } else {
62
            $subject = 'newpage';
63
            $trep['OLDPAGE'] = '---';
64
            $tdiff = rawWiki($id);
65
            $hdiff = nl2br(hsc($tdiff));
66
        }
67
68
        $trep['DIFF'] = $tdiff;
69
        $hrep['DIFF'] = $hdiff;
70
71
        $headers = ['Message-Id' => $this->getMessageID($id)];
72
        if ($rev) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rev of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
            $headers['In-Reply-To'] = $this->getMessageID($id, $rev);
74
        }
75
76
        return $this->send(
77
            $subscriber_mail,
78
            $subject,
79
            $id,
80
            $template,
81
            $trep,
82
            $hrep,
83
            $headers
84
        );
85
    }
86
87
}
88