PageSubscriptionSender::sendPageDiff()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 6
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
     * @param int|null $current_rev     New revision if any
23
     *
24
     * @return bool                     true if successfully sent
25
     */
26
    public function sendPageDiff($subscriber_mail, $template, $id, $rev = null, $summary = '', $current_rev = null)
27
    {
28
        global $DIFF_INLINESTYLES;
29
30
        // prepare replacements (keys not set in hrep will be taken from trep)
31
        $trep = [
32
            'PAGE' => $id,
33
            'NEWPAGE' => wl($id, $current_rev?('rev='.$current_rev):'', true, '&'),
34
            'SUMMARY' => $summary,
35
            'SUBSCRIBE' => wl($id, ['do' => 'subscribe'], true, '&'),
36
        ];
37
        $hrep = [];
38
39
        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...
40
            $subject = 'changed';
41
            $trep['OLDPAGE'] = wl($id, "rev=$rev", true, '&');
42
43
            $old_content = rawWiki($id, $rev);
44
            $new_content = rawWiki($id);
45
46
            $df = new Diff(
47
                explode("\n", $old_content),
48
                explode("\n", $new_content)
49
            );
50
            $dformat = new UnifiedDiffFormatter();
51
            $tdiff = $dformat->format($df);
52
53
            $DIFF_INLINESTYLES = true;
54
            $df = new Diff(
55
                explode("\n", $old_content),
56
                explode("\n", $new_content)
57
            );
58
            $dformat = new InlineDiffFormatter();
59
            $hdiff = $dformat->format($df);
60
            $hdiff = '<table>' . $hdiff . '</table>';
61
            $DIFF_INLINESTYLES = false;
62
        } else {
63
            $subject = 'newpage';
64
            $trep['OLDPAGE'] = '---';
65
            $tdiff = rawWiki($id);
66
            $hdiff = nl2br(hsc($tdiff));
67
        }
68
69
        $trep['DIFF'] = $tdiff;
70
        $hrep['DIFF'] = $hdiff;
71
72
        $headers = ['Message-Id' => $this->getMessageID($id)];
73
        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...
74
            $headers['In-Reply-To'] = $this->getMessageID($id, $rev);
75
        }
76
77
        return $this->send(
78
            $subscriber_mail,
79
            $subject,
80
            $id,
81
            $template,
82
            $trep,
83
            $hrep,
84
            $headers
85
        );
86
    }
87
88
}
89