Version3   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 62.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 33
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 12 12 3
A _getAdvancedSessionValidation() 7 7 2
A _getTemplateVars() 13 13 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
/**
4
 * Nexcess.net Turpentine Extension for Magento
5
 * Copyright (C) 2012  Nexcess.net L.L.C.
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
class Nexcessnet_Turpentine_Model_Varnish_Configurator_Version3
23
    extends Nexcessnet_Turpentine_Model_Varnish_Configurator_Abstract {
24
25
    const VCL_TEMPLATE_FILE = 'version-3.vcl';
26
    const VCL_VERSION = '3';
27
28
29
    /**
30
     * Generate the Varnish 3.0-compatible VCL
31
     *
32
     * @param bool $doClean if true, VCL will be cleaned (whitespaces stripped, etc.)
33
     * @return string
34
     */
35 View Code Duplication
    public function generate($doClean = true) {
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...
36
        // first, check if a custom template is set
37
        $customTemplate = $this->_getCustomTemplateFilename();
38
        if ($customTemplate) { 
0 ignored issues
show
Bug Best Practice introduced by
The expression $customTemplate of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39
            $tplFile = $customTemplate;
40
        } else { 
41
            $tplFile = $this->_getVclTemplateFilename(self::VCL_TEMPLATE_FILE);
42
        }
43
        $vcl = $this->_formatTemplate(file_get_contents($tplFile),
44
            $this->_getTemplateVars());
45
        return $doClean ? $this->_cleanVcl($vcl) : $vcl;
46
    }
47
48 View Code Duplication
    protected function _getAdvancedSessionValidation() {
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...
49
        $validation = '';
50
        foreach ($this->_getAdvancedSessionValidationTargets() as $target) {
51
            $validation .= sprintf('hash_data(%s);'.PHP_EOL, $target);
52
        }
53
        return $validation;
54
    }
55
56
    /**
57
     * Build the list of template variables to apply to the VCL template
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    protected function _getTemplateVars() {
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...
62
        $vars = parent::_getTemplateVars();
63
        $vars['advanced_session_validation'] =
64
            $this->_getAdvancedSessionValidation();
65
66
        //dispatch event to allow other extensions to add custom vcl template variables
67
        Mage::dispatchEvent('turpentine_get_templatevars_after', array(
68
            'vars' => &$vars,
69
            'vcl_version'=> self::VCL_VERSION
70
        ));
71
72
        return $vars;
73
    }
74
}
75