Completed
Push — master ( 603564...3d1dbb )
by Michael
03:27
created

Console_GetoptPlus_Exception   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 51 and the first side effect is on line 37.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Console GetoptPlus/Exception
5
 *
6
 * All rights reserved.
7
 * Redistribution and use in source and binary forms, with or without modification,
8
 * are permitted provided that the following conditions are met:
9
 * + Redistributions of source code must retain the above copyright notice,
10
 * this list of conditions and the following disclaimer.
11
 * + Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation and/or
13
 * other materials provided with the distribution.
14
 * + The names of its contributors may not be used to endorse or promote
15
 * products derived from this software without specific prior written permission.
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 * @category  Console
29
 * @package   Console_GetoptPlus
30
 * @author    Michel Corne <[email protected]>
31
 * @copyright 2008 Michel Corne
32
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
33
 * @version   SVN: $Id: Exception.php 47 2008-01-10 11:03:38Z mcorne $
34
 * @link      http://pear.php.net/package/Console_GetoptPlus
35
 */
36
37
require_once 'PEAR/Exception.php';
38
39
/**
40
 * Handling of error messages and exceptions.
41
 *
42
 * @category  Console
43
 * @package   Console_GetoptPlus
44
 * @author    Michel Corne <[email protected]>
45
 * @copyright 2008 Michel Corne
46
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
47
 * @version   Release: @package_version@
48
 * @link      http://pear.php.net/package/Console_GetoptPlus
49
 * @see       PEAR_Exception
50
 */
51
class Console_GetoptPlus_Exception extends PEAR_Exception
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
52
{
53
    /**
54
     * The error messages
55
     *
56
     * @var    array
57
     * @access private
58
     */
59
    private $messages = array(// /
60
        'unknow' => array(1, 'Console_Getopt: unknown error ID (%s)'),
61
        // original Console_Getopt error messages
62
        'ambigous' => array(10, 'Console_Getopt: option --%s is ambiguous'),
63
        'mandatory' => array(11, 'Console_Getopt: option requires an argument --%s'),
64
        'noargument' => array(12, 'Console_Getopt: option --%s doesn\'t allow an argument'),
65
        'noargs' => array(13, 'Console_Getopt: Could not read cmd args (register_argc_argv=Off?)'),
66
        'unrecognized' => array(14, 'Console_Getopt: unrecognized option --%s'),
67
        // additional Console_GetoptPlus_Getopt error messages
68
        'duplicate' => array(20, 'Console_Getopt: duplicate option name definition --%s'),
69
        'invalid' => array(21, 'Console_Getopt: invalid long option definition %s'),
70
        'string' => array(22, 'Console_Getopt: short options definition must be a string'),
71
        'syntax' => array(23, 'Console_Getopt: short options definition syntax error %s'),
72
        // additional Console_GetoptPlus error messages
73
        'missing' => array(30, 'Console_GetoptPlus: unknown option name #%s'),
74
        'type' => array(31, 'Console_GetoptPlus: unknown option type %s'),
75
        'convert' => array(32, 'Console_GetoptPlus: wrong option name conversion %s'),
76
        );
77
78
    /**
79
     * Triggers the exception.
80
     *
81
     * @param  mixed  $exception the exception ID and optional message part,
82
     *                           e.g. "string" or array("invalid", '--foo')
83
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
84
     * @access public
85
     */
86
    public function __construct($exception)
87
    {
88
        // extracts the exception ID and message parameters
89
        settype($exception, 'array');
90
        $id = current($exception);
91
        // resets the exception ID if no corresponding message (programmatic error!)
92
        isset($this->messages[$id]) or $exception = array(null, $id) and $id = 'unknow';
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or 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...
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...
93
        // extracts the exception code and pattern
94
        list($code, $format) = $this->messages[$id];
95
        $exception[0] = $format;
96
        // completes the message, throws the exception
97
        $message = call_user_func_array('sprintf', $exception);
98
        parent::__construct($message, $code);
99
    }
100
}
101
102
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

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.

Loading history...
103