Test Failed
Push — master ( 6275ca...dcd74b )
by Vítězslav
03:26
created

update_formats_class.php ➔ getEvidenceFormats()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 3
nop 2
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
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 7 and the first side effect is on line 5.

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
namespace FlexiPeeHP;
4
5
require_once '../testing/bootstrap.php';
6
7
define('EASE_APPNAME', 'FlexiPeehUP');
8
define('EASE_LOGGER', 'console|syslog');
9
10
$outFile = 'Formats.php';
11
$ok      = 0;
12
13
/**
14
 * Obtain Formats for given evidence
15
 *
16
 * @param string     $evidence
17
 * @param FlexiBeeRO $syncer Class to read from FlexiBee
18
 * @return array     Formats structure
19
 */
20
function getEvidenceFormats($evidence, FlexiBeeRO $syncer)
21
{
22
23
    $syncer->setEvidence($evidence);
24
    $flexinfo = $syncer->getColumnsFromFlexibee(['id'], ['limit' => 1]);
25
    if (is_array($flexinfo) && array_key_exists('id', $flexinfo[0])) {
26
        $id = is_numeric($flexinfo[0]['id']) ? intval($flexinfo[0]['id']) : $flexinfo[0]['id'];
27
28
        foreach (FlexiBeeRO::$formats as $cancode => $candidate) {
29
            $syncer->setFormat($candidate['suffix']);
30
            $syncer->loadFromFlexiBee($id);
31
            if ($syncer->lastResponseCode == 200) {
32
                $formats[$cancode] = $candidate['suffix'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$formats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $formats = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
33
            }
34
        }
35
    } else {
36
        $syncer->addStatusMessage(sprintf('Missing formats for %s', $evidence),
37
            'warning');
38
        $formats = ['HTML' => 'html', 'XML' => 'xml', 'JSON' => 'json', 'CSV' => 'csv'];
39
    }
40
    return $formats;
0 ignored issues
show
Bug introduced by
The variable $formats does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
41
}
42
$evidenceFormats = '<?php
43
/**
44
 * FlexiPeeHP - Evidence Formats.
45
 *
46
 * @author     Vítězslav Dvořák <[email protected]>
47
 * @copyright  (C) 2015-2017 Spoje.Net
48
 */
49
namespace FlexiPeeHP;
50
51
/**
52
 * Evidence Formats
53
 *
54
 * @link https://demo.flexibee.eu/devdoc/formats Provádění akcí
55
 */
56
class Formats
57
{
58
';
59
$statuser        = new Status();
60
$evidenceFormats .= ' /**
61
 * Source FlexiBee server version.
62
 *
63
 * @var string
64
 */
65
';
66
$evidenceFormats .= ' static public $version = \''.$statuser->getDataValue('version').'\';
67
68
';
69
70
71
$syncer = new FlexiBeeRO();
72
$syncer->addStatusMessage('Updating Evidences Formats');
73
74
$pos = 0;
75 View Code Duplication
foreach (EvidenceList::$name as $evidencePath => $evidenceName) {
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...
76
    $pos++;
77
    $structure = getEvidenceFormats($evidencePath, $syncer);
78
    if (count($structure)) {
79
        $evidenceFormats .= '    /**
80
     * Evidence '.$evidencePath.' ('.$evidenceName.') Formats.
81
     *
82
     * @var array
83
     */
84
';
85
        $evidenceFormats .= ' static public $'.lcfirst(FlexiBeeRO::evidenceToClassName($evidencePath)).' = '.var_export($structure,
86
                true).';
87
';
88
89
        $syncer->addStatusMessage($pos.' of '.count(EvidenceList::$name).' '.$evidencePath.': formats: '.implode(',',
90
                $structure).' obtained', 'success');
91
        $ok++;
92
    } else {
93
        $syncer->addStatusMessage($pos.' of '.count(EvidenceList::$name).' '.$evidencePath.': obtaining formats problem',
94
            'error');
95
    }
96
}
97
98
$evidenceFormats .= '}
99
';
100
101
$syncer->addStatusMessage('Updating of '.$ok.' Evidences Formats done',
102
    'success');
103
file_put_contents($outFile, $evidenceFormats);
104