function.icon.php ➔ Smarty_function_icon()   F
last analyzed

Complexity

Conditions 16
Paths 384

Size

Total Lines 94
Code Lines 43

Duplication

Lines 5
Ratio 5.32 %

Importance

Changes 0
Metric Value
cc 16
eloc 43
c 0
b 0
f 0
nc 384
nop 1
dl 5
loc 94
rs 3.7109

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Smarty plugin.
5
 */
6
7
/**
8
 * Name:    icon
9
 * Type:    function
10
 * Purpose: This TAG inserts images/icons.
11
 *
12
 * Static Function to Call variable Methods from templates via
13
 * {icon ...}
14
 * Parameters: icondir, src, width, heigth, alt, extra
15
 *
16
 * Example Usage:
17
 * {icon name="rss"}
18
 * {icon theme="lullacons" name="calendar"}
19
 *
20
 * @param array $params as described above
21
 *
22
 * @return string
23
 */
24
function Smarty_function_icon($params)
0 ignored issues
show
Coding Style introduced by
function Smarty_function_icon() 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...
Coding Style introduced by
Smarty_function_icon uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
{
26
    $src    = '';
27
    $height = '';
28
    $alt    = '';
29
    $name   = '';
30
    $width  = '';
31
    $extra  = '';
32
33
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
34
      @todo provide usage help text in error message
35
      if (empty($params['name']) and empty($params['src'])) {
36
      trigger_error('Provide "name" or "src".', E_USER_ERROR);
37
38
      return;
39
      } */
40
41
    extract($params);
42
43
    /*
44
     * if the src attribute contains a http://SERVER_NAME URL its cutted of
45
     */
46
    if (isset($src) and empty($src) === false) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
47
        $needle = 'http://' . $_SERVER['SERVER_NAME'] . '/';
48
        $pos    = mb_strpos($src, $needle);
49
        if ($src !== null and is_int($pos)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
50
            #\Koch\Debug\Debug::printR($pos);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
51
            $src  = mb_substr($src, $pos + mb_strlen($needle));
52
            $name = basename($src);
53
        }
54
    }
55
56
    // we have two alternatives :
57
    // a) src => user has set src, defining the path to the image and imagename
58
    // b) icondir, name => user has defined the icons dir (relative to core/images folder) and the name of a png file
59
    // check if it is a valid one
60
    $icondir_whitelist = ['icons', 'lullacons'];
0 ignored issues
show
Coding Style introduced by
$icondir_whitelist 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...
61
    if ((isset($icondir)) and in_array($icondir, $icondir_whitelist, true)) {
0 ignored issues
show
Coding Style introduced by
$icondir_whitelist 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...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62
        // valid
63
        $icondir .= ''; // leave this. would else be an empty if statement
64
    } else { // fallback to a valid default
65
        $icondir = 'icons';
66
    }
67
68
    // transform name into a valid image src
69
    $src = realpath(APPLICATION_PATH . 'themes/' . 'core/images/' . $icondir . DIRECTORY_SEPARATOR . $name . '.png');
70
71
    // if we got no valid src, set a default image
72
    if (isset($src) and is_file($src) === false) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
73
        #$src = WWW_ROOT_THEMES_CORE . 'images/noimage.gif';
74
        $src  = APPLICATION_PATH . 'themes/' . 'core/images/noimage.gif';
75
        $name = 'No Image found.' . $src;
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
    }
77
78
    // we got no height, set it to zero
79
    if (empty($height)) {
80
        $height = 0;
81
    }
82
83
    // we got no width, ok then its zero again
84
    if (empty($width)) {
85
        $width = 0;
86
    }
87
88
    // we got no height nor width. well let's detect it automatically then.
89 View Code Duplication
    if (($height === 0) || ($width === 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90
        $currentimagesize = getimagesize($src);
91
        $width            = $currentimagesize[0];
92
        $height           = $currentimagesize[1];
93
    }
94
95
    // we got no alternative text. let's add a default text with $name;
96
    if (($src !== null) and empty($alt)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
97
        $file = $src;
98
99
        $info      = pathinfo($file);
100
        $file_name = basename($file, '.' . $info['extension']);
0 ignored issues
show
Coding Style introduced by
$file_name 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...
101
        $alt       = $file_name;
0 ignored issues
show
Coding Style introduced by
$file_name 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...
102
    }
103
104
    // no extra attributes to add, then let it be an empty string
105
    if (empty($extra)) {
106
        $extra = '';
107
    }
108
109
    // prepare link: transform absolute path into webpath and apply slashfix
110
    $src = str_replace(APPLICATION_PATH . 'themes/', WWW_ROOT_THEMES, $src);
111
    $src = str_replace('\\', '/', $src);
112
113
    $html = '<img src="' . $src . '" height="' . $height . '" width="'
114
        . $width . '" alt="' . $alt . '" ' . $extra . ' />';
115
116
    return $html;
117
}
118