Xtemplate::clearCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\View\Renderer;
26
27
use Koch\View\AbstractRenderer;
28
29
/**
30
 * Koch Framework - View Renderer for Xtemplate templates.
31
 *
32
 * This is a wrapper/adapter for rendering with XTemplate.
33
 *
34
 * @link http://www.phpxtemplate.org/ Offical Website of PHP XTemplate
35
 * @link http://xtpl.sourceforge.net/ Project's Website at Sourceforge
36
 */
37
class Xtemplate extends AbstractRenderer
38
{
39
    /* @var \XTemplate */
40
    public $renderer = null;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param array $options
46
     */
47
    public function __construct($options = [])
48
    {
49
        parent::__construct($options);
50
    }
51
52
    public function initializeEngine($template = null)
53
    {
54
        $xtpl = VENDOR_PATH . '/xtemplate/xtemplate.class.php';
55
56
        // prevent redeclaration
57
        if (!class_exists('XTemplate', false)) {
58
            // check if library exists
59
            if (is_file($xtpl)) {
60
                include $xtpl;
61
            } else {
62
                throw new \Exception('The vendor library "XTemplate" is required.');
63
            }
64
        }
65
66
        $template = $this->getTemplatePath($template);
0 ignored issues
show
Documentation Bug introduced by
The method getTemplatePath does not exist on object<Koch\View\Renderer\Xtemplate>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
68
        #\Koch\Debug\Debug::firebug('Xtemplate loaded with Template: ' . $template);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
70
        // Do it with XTemplate style > eat like a bird, poop like an elefant!
71
        return $this->renderer = new self($template);
0 ignored issues
show
Documentation Bug introduced by
It seems like new self($template) of type object<Koch\View\Renderer\Xtemplate> is incompatible with the declared type object<XTemplate> of property $renderer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug Best Practice introduced by
The return type of return $this->renderer = new self($template); (Koch\View\Renderer\Xtemplate) is incompatible with the return type declared by the abstract method Koch\View\AbstractRenderer::initializeEngine of type Koch\View\Engine.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
72
    }
73
74
    public function configureEngine()
75
    {
76
    }
77
78
    public function renderPartial($template)
0 ignored issues
show
Unused Code introduced by
The parameter $template is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
    }
81
82
    public function clearVars()
83
    {
84
    }
85
86
    public function clearCache()
87
    {
88
    }
89
90
    public function fetch($template, $data = null)
91
    {
92
    }
93
94
    public function display($template, $data = null)
95
    {
96
    }
97
98
    /**
99
     * Returns a clean xTemplate Object.
100
     *
101
     * @return Xtemplate Object
0 ignored issues
show
Documentation introduced by
Should the return type not be \XTemplate?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
102
     */
103
    public function getEngine()
104
    {
105
        // clear assigns?
106
        return $this->renderer;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderer; (XTemplate) is incompatible with the return type of the parent method Koch\View\AbstractRenderer::getEngine of type Koch\View\Renderer|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
107
    }
108
109
    public function render($template = null, $viewdata = null)
110
    {
111
        $this->renderer->assign($viewdata);
112
        $this->renderer->parse($template);
113
        $this->renderer->out($template);
114
    }
115
116
    public function assign($tpl_parameter, $value = null)
117
    {
118
        $this->renderer->assign($tpl_parameter, $value);
0 ignored issues
show
Coding Style introduced by
$tpl_parameter does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
119
    }
120
}
121