Completed
Push — develop ( 685e29...09d456 )
by Adrien
14:14
created

Logical::true()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace PhpSpreadsheet\Calculation;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category    PhpSpreadsheet
23
 * @copyright    Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 * @version        ##VERSION##, ##DATE##
26
 */
27
class Logical
28
{
29
    /**
30
     * TRUE
31
     *
32
     * Returns the boolean TRUE.
33
     *
34
     * Excel Function:
35
     *        =TRUE()
36
     *
37
     * @category Logical Functions
38
     * @return    bool        True
39
     */
40
    public static function true()
41 1
    {
42
        return true;
43 1
    }
44
45
    /**
46
     * FALSE
47
     *
48
     * Returns the boolean FALSE.
49
     *
50
     * Excel Function:
51
     *        =FALSE()
52
     *
53
     * @category Logical Functions
54
     * @return    bool        False
55
     */
56
    public static function false()
57
    {
58
        return false;
59 1
    }
60
61 1
    /**
62
     * LOGICAL_AND
63
     *
64
     * Returns boolean TRUE if all its arguments are TRUE; returns FALSE if one or more argument is FALSE.
65
     *
66
     * Excel Function:
67
     *        =AND(logical1[,logical2[, ...]])
68
     *
69
     *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
70
     *            or references that contain logical values.
71
     *
72
     *        Boolean arguments are treated as True or False as appropriate
73
     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
74
     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
75
     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
76
     *
77
     * @category Logical Functions
78
     * @param    mixed        $arg,...        Data values
0 ignored issues
show
Bug introduced by
There is no parameter named $arg,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
     * @return    bool        The logical AND of the arguments.
80
     */
81
    public static function logicalAnd()
82
    {
83
        // Return value
84
        $returnValue = true;
85
86 21
        // Loop through the arguments
87
        $aArgs = Functions::flattenArray(func_get_args());
88
        $argCount = -1;
89 21
        foreach ($aArgs as $argCount => $arg) {
90
            // Is it a boolean value?
91
            if (is_bool($arg)) {
92 21
                $returnValue = $returnValue && $arg;
93 21
            } elseif ((is_numeric($arg)) && (!is_string($arg))) {
94 21
                $returnValue = $returnValue && ($arg != 0);
95 View Code Duplication
            } elseif (is_string($arg)) {
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...
96 20
                $arg = strtoupper($arg);
97 10
                if (($arg == 'TRUE') || ($arg == \PhpSpreadsheet\Calculation::getTRUE())) {
98 13
                    $arg = true;
99 7
                } elseif (($arg == 'FALSE') || ($arg == \PhpSpreadsheet\Calculation::getFALSE())) {
100 7
                    $arg = false;
101 4
                } else {
102 4
                    return Functions::VALUE();
103 1
                }
104 3
                $returnValue = $returnValue && ($arg != 0);
105 1
            }
106
        }
107 2
108
        // Return
109 18
        if ($argCount < 0) {
110
            return Functions::VALUE();
111
        }
112
113
        return $returnValue;
114 19
    }
115 1
116
    /**
117 18
     * LOGICAL_OR
118
     *
119
     * Returns boolean TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.
120
     *
121
     * Excel Function:
122
     *        =OR(logical1[,logical2[, ...]])
123
     *
124
     *        The arguments must evaluate to logical values such as TRUE or FALSE, or the arguments must be arrays
125
     *            or references that contain logical values.
126
     *
127
     *        Boolean arguments are treated as True or False as appropriate
128
     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
129
     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
130
     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
131
     *
132
     * @category Logical Functions
133
     * @param    mixed        $arg,...        Data values
0 ignored issues
show
Bug introduced by
There is no parameter named $arg,.... Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
134
     * @return    bool        The logical OR of the arguments.
135
     */
136
    public static function logicalOr()
137
    {
138
        // Return value
139
        $returnValue = false;
140
141
        // Loop through the arguments
142 20
        $aArgs = Functions::flattenArray(func_get_args());
143
        $argCount = -1;
144
        foreach ($aArgs as $argCount => $arg) {
145 20
            // Is it a boolean value?
146
            if (is_bool($arg)) {
147
                $returnValue = $returnValue || $arg;
148 20
            } elseif ((is_numeric($arg)) && (!is_string($arg))) {
149 20
                $returnValue = $returnValue || ($arg != 0);
150 20 View Code Duplication
            } elseif (is_string($arg)) {
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...
151
                $arg = strtoupper($arg);
152 19
                if (($arg == 'TRUE') || ($arg == \PhpSpreadsheet\Calculation::getTRUE())) {
153 10
                    $arg = true;
154 12
                } elseif (($arg == 'FALSE') || ($arg == \PhpSpreadsheet\Calculation::getFALSE())) {
155 7
                    $arg = false;
156 6
                } else {
157 3
                    return Functions::VALUE();
158 3
                }
159 1
                $returnValue = $returnValue || ($arg != 0);
160 2
            }
161 1
        }
162
163 1
        // Return
164
        if ($argCount < 0) {
165 18
            return Functions::VALUE();
166
        }
167
168
        return $returnValue;
169
    }
170 19
171 1
    /**
172
     * NOT
173 18
     *
174
     * Returns the boolean inverse of the argument.
175
     *
176
     * Excel Function:
177
     *        =NOT(logical)
178
     *
179
     *        The argument must evaluate to a logical value such as TRUE or FALSE
180
     *
181
     *        Boolean arguments are treated as True or False as appropriate
182
     *        Integer or floating point arguments are treated as True, except for 0 or 0.0 which are False
183
     *        If any argument value is a string, or a Null, the function returns a #VALUE! error, unless the string holds
184
     *            the value TRUE or FALSE, in which case it is evaluated as the corresponding boolean value
185
     *
186
     * @category Logical Functions
187
     * @param    mixed        $logical    A value or expression that can be evaluated to TRUE or FALSE
188
     * @return    bool        The boolean inverse of the argument.
189
     */
190
    public static function NOT($logical = false)
191
    {
192
        $logical = Functions::flattenSingleValue($logical);
193
        if (is_string($logical)) {
194
            $logical = strtoupper($logical);
195
            if (($logical == 'TRUE') || ($logical == \PhpSpreadsheet\Calculation::getTRUE())) {
196
                return false;
197 20
            } elseif (($logical == 'FALSE') || ($logical == \PhpSpreadsheet\Calculation::getFALSE())) {
198
                return true;
199 20
            } else {
200 20
                return Functions::VALUE();
201 10
            }
202 10
        }
203 1
204 9
        return !$logical;
205 1
    }
206
207 8
    /**
208
     * STATEMENT_IF
209
     *
210
     * Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.
211 10
     *
212
     * Excel Function:
213
     *        =IF(condition[,returnIfTrue[,returnIfFalse]])
214
     *
215
     *        Condition is any value or expression that can be evaluated to TRUE or FALSE.
216
     *            For example, A10=100 is a logical expression; if the value in cell A10 is equal to 100,
217
     *            the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE.
218
     *            This argument can use any comparison calculation operator.
219
     *        ReturnIfTrue is the value that is returned if condition evaluates to TRUE.
220
     *            For example, if this argument is the text string "Within budget" and the condition argument evaluates to TRUE,
221
     *            then the IF function returns the text "Within budget"
222
     *            If condition is TRUE and ReturnIfTrue is blank, this argument returns 0 (zero). To display the word TRUE, use
223
     *            the logical value TRUE for this argument.
224
     *            ReturnIfTrue can be another formula.
225
     *        ReturnIfFalse is the value that is returned if condition evaluates to FALSE.
226
     *            For example, if this argument is the text string "Over budget" and the condition argument evaluates to FALSE,
227
     *            then the IF function returns the text "Over budget".
228
     *            If condition is FALSE and ReturnIfFalse is omitted, then the logical value FALSE is returned.
229
     *            If condition is FALSE and ReturnIfFalse is blank, then the value 0 (zero) is returned.
230
     *            ReturnIfFalse can be another formula.
231
     *
232
     * @category Logical Functions
233
     * @param    mixed    $condition        Condition to evaluate
234
     * @param    mixed    $returnIfTrue    Value to return when condition is true
235
     * @param    mixed    $returnIfFalse    Optional value to return when condition is false
236
     * @return    mixed    The value of returnIfTrue or returnIfFalse determined by condition
237
     */
238
    public static function statementIf($condition = true, $returnIfTrue = 0, $returnIfFalse = false)
239
    {
240
        $condition = (is_null($condition))     ? true :  (boolean) Functions::flattenSingleValue($condition);
241
        $returnIfTrue = (is_null($returnIfTrue))  ? 0 :     Functions::flattenSingleValue($returnIfTrue);
242
        $returnIfFalse = (is_null($returnIfFalse)) ? false : Functions::flattenSingleValue($returnIfFalse);
243
244
        return ($condition) ? $returnIfTrue : $returnIfFalse;
245
    }
246 15
247
    /**
248 15
     * IFERROR
249 15
     *
250 15
     * Excel Function:
251
     *        =IFERROR(testValue,errorpart)
252 15
     *
253
     * @category Logical Functions
254
     * @param    mixed    $testValue    Value to check, is also the value returned when no error
255
     * @param    mixed    $errorpart    Value to return when testValue is an error condition
256
     * @return    mixed    The value of errorpart or testValue determined by error condition
257
     */
258
    public static function IFERROR($testValue = '', $errorpart = '')
259
    {
260
        $testValue = (is_null($testValue)) ? '' : Functions::flattenSingleValue($testValue);
261
        $errorpart = (is_null($errorpart)) ? '' : Functions::flattenSingleValue($errorpart);
262
263
        return self::statementIf(Functions::isError($testValue), $errorpart, $testValue);
264
    }
265
}
266