| Conditions | 3 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 3 | Features | 2 |
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 |
||
| 43 | public function __construct($file, Registry $registry = null) { |
||
| 44 | if ($registry == null) { |
||
| 45 | $registry = Registry::singleton(); |
||
| 46 | } |
||
| 47 | |||
| 48 | //initialize core if neccessary |
||
| 49 | self::initCoreIfAbsent(); |
||
| 50 | |||
| 51 | //find file |
||
| 52 | $this->file = Template::findTemplate($file, $registry); |
||
| 53 | |||
| 54 | //load a template file, this is reusable if you want to render multiple times the same template with different data |
||
| 55 | if (isset(self::$files[$file])) { |
||
| 56 | $this->template = self::$files[$this->file]; |
||
| 57 | } else { |
||
| 58 | $this->template = new Dwoo\Template\File($this->file); |
||
| 59 | self::$files[$file] = $this->template; |
||
| 60 | } |
||
| 61 | |||
| 62 | //create a data set, this data set can be reused to render multiple templates if it contains enough data to fill them all |
||
| 63 | $this->data = new Dwoo\Data(); |
||
| 64 | |||
| 65 | //set default values |
||
| 66 | $this->assign("REGISTRY", $registry->listSettings()); |
||
| 67 | |||
| 68 | //set CSRF token |
||
| 69 | $this->assign("CSRF_TOKEN", Security::getCSRFToken()); |
||
| 70 | |||
| 71 | //set domain, current page and so on |
||
| 72 | $this->assign("DOMAIN", DomainUtils::getCurrentDomain()); |
||
| 73 | $this->assign("BASE_URL", DomainUtils::getBaseURL()); |
||
| 74 | $this->assign("CURRENT_URL", DomainUtils::getURL()); |
||
| 75 | $this->assign("FOLDER", $registry->getSetting("folder")); |
||
| 76 | |||
| 77 | //set language settings |
||
| 78 | $this->assign("PREF_LANG", $registry->getSetting("pref_lang")); |
||
| 79 | $this->assign("LANG_TOKEN", $registry->getSetting("lang_token")); |
||
| 80 | |||
| 81 | $domain = $registry->getObject("domain"); |
||
| 82 | $this->assign("HOME_PAGE", $domain->getHomePage()); |
||
| 83 | $this->assign("LOGIN_PAGE", Settings::get("login_page", "login")); |
||
| 84 | $this->assign("LOGIN_URL", $registry->getSetting("login_url")); |
||
| 85 | $this->assign("LOGOUT_PAGE", Settings::get("logout_page", "logout")); |
||
| 86 | $this->assign("LOGOUT_URL", $registry->getSetting("logout_url")); |
||
| 87 | |||
| 88 | //set user variables |
||
| 89 | $this->assign("USERID", User::current()->getID()); |
||
| 90 | $this->assign("USERNAME", User::current()->getUsername()); |
||
| 91 | |||
| 92 | $style_name = $registry->getSetting("current_style_name"); |
||
| 93 | $this->assign("STYLE_PATH",DomainUtils::getBaseURL() . "/styles/" . $style_name . "/"); |
||
| 94 | } |
||
| 164 |
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.
A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.