Completed
Push — master ( c52c5c...9fd6d3 )
by
unknown
19s queued 10s
created

AbstractTemplate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 8
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A templateName() 0 23 3
A init() 0 5 1
A setDependencies() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Charcoal\App\Template;
4
5
// From 'psr/log'
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerAwareTrait;
8
9
// From 'psr/http-message'
10
use Psr\Http\Message\RequestInterface;
11
12
// From 'pimple/pimple'
13
use Pimple\Container;
14
15
// From 'charcoal-config'
16
use Charcoal\Config\AbstractEntity;
17
18
// From 'charcoal-app'
19
use Charcoal\App\Template\TemplateInterface;
20
21
/**
22
 * Template (View Controller) base class
23
 */
24
abstract class AbstractTemplate extends AbstractEntity implements
25
    LoggerAwareInterface,
26
    TemplateInterface
27
{
28
    use LoggerAwareTrait;
29
30
    /**
31
     * The cache of parsed template names.
32
     *
33
     * @var array
34
     */
35
    protected static $templateNameCache = [];
36
37
    /**
38
     * @param array|\ArrayAccess $data The dependencies (app and logger).
39
     */
40 View Code Duplication
    public function __construct($data = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $this->setLogger($data['logger']);
43
44
        if (isset($data['container'])) {
45
            $this->setDependencies($data['container']);
46
        }
47
    }
48
49
    /**
50
     * Retrieve the template's identifier.
51
     *
52
     * @return string
53
     */
54
    public function templateName()
55
    {
56
        $key = substr(strrchr('\\'.get_class($this), '\\'), 1);
57
58
        if (!isset(static::$templateNameCache[$key])) {
59
            $value = $key;
60
61
            if (!ctype_lower($value)) {
62
                $value = preg_replace('/\s+/u', '', $value);
63
                $value = mb_strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1-', $value), 'UTF-8');
64
            }
65
66
            $value = str_replace(
67
                [ 'abstract', 'trait', 'interface', 'template', '\\' ],
68
                '',
69
                $value
70
            );
71
72
            static::$templateNameCache[$key] = trim($value, '-');
73
        }
74
75
        return static::$templateNameCache[$key];
76
    }
77
78
    /**
79
     * Initialize the template with a request.
80
     *
81
     * @param RequestInterface $request The request to intialize.
82
     * @return boolean Success / Failure.
83
     */
84
    public function init(RequestInterface $request)
85
    {
86
        // This method is a stub. Reimplement in children methods to ensure template initialization.
87
        return true;
88
    }
89
90
    /**
91
     * Give an opportunity to children classes to inject dependencies from a Pimple Container.
92
     *
93
     * Does nothing by default, reimplement in children classes.
94
     *
95
     * The `$container` DI-container (from `Pimple`) should not be saved or passed around, only to be used to
96
     * inject dependencies (typically via setters).
97
     *
98
     * @param Container $container A dependencies container instance.
99
     * @return void
100
     */
101
    protected function setDependencies(Container $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container 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...
102
    {
103
        // This method is a stub. Reimplement in children template classes.
104
    }
105
}
106