Passed
Push — master ( 005d4f...8b5e26 )
by Michael
06:49
created

CountOverload   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCount() 0 3 1
A getCounts() 0 6 1
A castType() 0 8 2
1
<?php
2
3
namespace XoopsModules\Pedigree;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
/**
15
 *
16
 * @package         XoopsModules\Pedigree
17
 * @author          XOOPS Development Team - <https://xoops.org>
18
 * @copyright       {@link https://xoops.org/ XOOPS Project}
19
 * @license         GPL 2.0 or later
20
 * @link            https://xoops.org/
21
 * @since           1.32
22
 */
23
24
/**
25
 * Class Trait to overload XoopsObject getCount() & getCounts() methods because XOOPS
26
 * doesn't return int as expected in XOOPS <= 2.5.10, need to check in later versions
27
 *
28
 * To use: add 'use CountOverload' in the class that extends \XoopsPersistableObjectHandler
29
 *
30
 **/
31
trait CountOverload
32
{
33
    /**
34
     * count objects matching a condition
35
     *
36
     * Overload of XoopsObject getCount() because XOOPS doesn't return int as expected
37
     * in XOOPS <= 2.5.10, need to check in later versions
38
     *
39
     * @param  \CriteriaElement|null $criteria {@link \CriteriaElement} to match
40
     * @return int count of objects
41
     */
42
    public function getCount(?\CriteriaElement $criteria = null): int
0 ignored issues
show
Unused Code introduced by
The parameter $criteria is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public function getCount(/** @scrutinizer ignore-unused */ ?\CriteriaElement $criteria = null): int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        return (int) parent::getCount($criteria = null);
45
    }
46
47
    /**
48
     * Get counts of objects matching a condition
49
     *
50
     * @param  \CriteriaElement|null $criteria {@link CriteriaElement} to match
51
     * @return int[] of counts
52
     */
53
    public function getCounts(?\CriteriaElement $criteria = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $criteria is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function getCounts(/** @scrutinizer ignore-unused */ ?\CriteriaElement $criteria = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        $countArray = parent::getCounts($criteria = null);
56
        array_walk($countArray, 'self::castType', 'int');
57
58
        return $countArray;
59
    }
60
61
    /**
62
     * Cast a variable to a known type
63
     *
64
     * @param mixed $var variable to cast (by reference)
65
     * @param string $type new type
66
     * @return bool true if type set was successful, false otherwise
67
     */
68
    private static function castType(&$var, $key,  ?string $type = 'int'): bool
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
    private static function castType(&$var, /** @scrutinizer ignore-unused */ $key,  ?string $type = 'int'): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $validTypes = ['bool', 'boolean', 'int', 'integer', 'float', 'double', 'string', 'array', 'object', 'null'];
71
        $success    = false;
72
        if (in_array($type, $validTypes)) {
73
            $success = settype($var, $type);
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $type of settype() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $success = settype($var, /** @scrutinizer ignore-type */ $type);
Loading history...
74
        }
75
        return $success;
76
    }
77
}
78