Conditions | 6 |
Paths | 32 |
Total Lines | 66 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
4 | function __construct() { |
||
5 | /* $_GET */ |
||
6 | if (holt45::chkGet("q")) { echo '$_GET["q"] is set'; } |
||
7 | |||
8 | echo holt45::assignFromGet("q"); // "" or $_GET["q"] |
||
9 | |||
10 | if (holt45::chkGetAll(array("q", "search"))) { echo '$_GET["q"] and $_GET["search"] is set'; } |
||
11 | 11111 |
||
12 | /* $_POST */ |
||
13 | if (holt45::chkPost("q")) { echo '$_POST["q"] is set'; } |
||
14 | |||
15 | echo holt45::assignFromPost("q"); // "" or $_POST["q"] |
||
16 | |||
17 | if (holt45::chkPostAll(array("q", "search"))) { echo '$_POST["q"] and $_POST["search"] is set'; } |
||
18 | |||
19 | /* Sessions */ |
||
20 | holt45::sessionSet("example_session_name", "content of session", 86400); |
||
21 | |||
22 | if (holt45::sessionIsset("example_session_name")) { echo 'Session example_session_name is set and not expired'; } |
||
23 | |||
24 | echo holt45::sessionRead("example_session_name"); // content of session |
||
25 | |||
26 | holt45::sessionDelete("example_session_name"); // Deletes session |
||
27 | |||
28 | /* Time */ |
||
29 | echo holt45::timestampToHttpDate("1980-01-01 17:15:00"); // Tue, 01 Jan 1980 16:15:00 GMT |
||
30 | |||
31 | echo holt45::timeElapsed("1980-01-01 17:15:00"); // 13173 days |
||
32 | |||
33 | /* Convert */ |
||
34 | echo holt45::rgbhex(array(255, 0, 0)); // ff0000 |
||
35 | |||
36 | print_r(holt45::hexrgb("#FF0000")); // Array([0] => 255, [1] => 0, [2] => 0) |
||
37 | |||
38 | /* Strings */ |
||
39 | echo holt45::textareaEncode('<textarea id="tex1"></textarea> <p> asdasd </p>'); // [textarea id="tex1"][/textarea] <p> asdasd </p> |
||
40 | |||
41 | echo holt45::textareaDecode('[textarea id="tex1"][/textarea] <p> asdasd </p>'); // <textarea id="tex1"></textarea> <p> asdasd </p> |
||
42 | |||
43 | echo holt45::obfuscateString("Hi, I'm a ninja!"); // 49574671626d6c75494745676253644a4943787053413d3d |
||
44 | |||
45 | echo holt45::deobfuscateString("49574671626d6c75494745676253644a4943787053413d3d"); // Hi, I'm a ninja! |
||
46 | |||
47 | echo holt45::replaceString("Hi my name is [@foo] and i like [@bar]", array("foo" => "sven", "bar" => "beer")); // Hi my name is sven and i like beer |
||
48 | |||
49 | echo holt45::rainbowText("Hallo world"); // <span style="color: #ff0000;">H</span><span style="color: #ff3300;">a</span>... |
||
50 | |||
51 | /* Math */ |
||
52 | print_r(holt45::generatePaginationRange(106, 15, 7)); // Array([0] => 1, [1] => 13, [2] => 14, [3] => 15, [4] => 16, [5] => 17, [6] => 106) |
||
53 | |||
54 | /* Misc */ |
||
55 | echo holt45::getClientIpAddress(); // 127.0.0.1 |
||
56 | |||
57 | print_r(holt45::urlParser("htt://w.google..com/")); // Array([url] => http://www.google.com/, [url_display] => www.google.com) |
||
58 | |||
59 | echo holt45::generatePassword(10); // 2k%=cbot:w |
||
60 | |||
61 | echo holt45::generatePassword(10, "simple"); // m9b7gfkmhc |
||
62 | |||
63 | echo holt45::iso3166ToName("SE"); // SWEDEN |
||
64 | |||
65 | /* constants */ |
||
66 | echo holt45::DATA_URI_TRANSPARENT_GIF; // data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7 |
||
67 | |||
68 | echo holt45::DATA_URI_TRANSPARENT_PNG; // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII= |
||
69 | } |
||
70 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.