FontResizer::resetfontsize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class FontResizer extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static $font_size_change = 0.1;
6
    public static function set_font_size_change($v)
7
    {
8
        self::$font_size_change = floatval($v);
9
    }
10
11
    protected static $max_font_size = 1.7;
12
    public static function set_max_font_size($v)
13
    {
14
        self::$max_font_size = floatval($v);
15
    }
16
17
    protected static $min_font_size = 0.7;
18
    public static function set_min_font_size($v)
19
    {
20
        self::$min_font_size = floatval($v);
21
    }
22
23
    public static $allowed_actions = array("increasefontsize", "decreasefontsize", "resetfontsize");
24
25
    // *** TEMPLATE FUNCTIONS
26
27
    public function CurrentFontSizeInPercentages($currentSize = 0)
28
    {
29
        Requirements::themedCSS("fontresizer");
30
        Requirements::javascript("fontresizer/javascript/fontresizer.js");
31
        Requirements::customScript("fontresizer.setMin(".(self::$min_font_size*100).");fontresizer.setMax(".(self::$max_font_size*100).");");
0 ignored issues
show
Documentation introduced by
'fontresizer.setMin(' . ..._font_size * 100 . ');' is of type string, but the function expects a object<The>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
        if (!$currentSize) {
33
            $currentSize = $this->currentFontSize();
34
        }
35
        return $currentSize * 100;
36
    }
37
38
    public function HasAlteredFont()
39
    {
40
        if ($this->currentFontSize() != 1) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $this->currentFontSize() != 1;.
Loading history...
41
            return true;
42
        }
43
        return false;
44
    }
45
46
47
    // *** ACTIONS
48
49
    public function increasefontsize()
50
    {
51
        return $this->fontAction(1 * self::$font_size_change);
52
    }
53
54
    public function decreasefontsize()
55
    {
56
        return $this->fontAction(-1 * self::$font_size_change);
57
    }
58
59
    public function resetfontsize()
60
    {
61
        Session::clear("fontsize");
62
        return $this->fontAction(0);
63
    }
64
65
66
    // *** INTERNAL FUNCTIONS
67
68
    protected function fontAction($change)
69
    {
70
        $currentSize = $this->currentFontSize();
71
        $currentSize = floatval($currentSize) + $change;
72
        Session::set("fontsize", $currentSize);
73
        if (Director::is_ajax()) {
74
            return $this->CurrentFontSizeInPercentages($currentSize);
75
        } else {
76
            Director::redirectBack();
0 ignored issues
show
Deprecated Code introduced by
The method Director::redirectBack() has been deprecated with message: 2.5 Use Controller->redirectBack()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
        }
78
        return array();
79
    }
80
81
82
    protected function currentFontSize()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
83
    {
84
        $currentSize = Session::get("fontsize");
85
        if (!$currentSize) {
86
            $currentSize = 1;
87
        }
88
        if ($currentSize < self::$min_font_size) {
89
            $currentSize = self::$min_font_size;
90
        }
91
        if ($currentSize > self::$max_font_size) {
92
            $currentSize = self::$max_font_size;
93
        }
94
        //make percentage
95
        return $currentSize;
96
    }
97
}
98