Completed
Push — master ( a139b5...cbfd2a )
by Vítězslav
04:33
created

UcetniObdobi   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
wmc 3
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A createYearsFrom() 0 17 3
1
<?php
2
/**
3
 * FlexiPeeHP - Objekt účetního období.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015,2016 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
/**
12
 * Class for Accounting period handling.
13
 *
14
 * @link https://demo.flexibee.eu/c/demo/ucetni-obdobi/properties Dokumentace
15
 * @author vitex
16
 */
17
class UcetniObdobi extends FlexiBee
18
{
19
    /**
20
     * Evidence FlexiBee
21
     * @var string
22
     */
23
    public $evidence = 'ucetni-obdobi';
24
25
    /**
26
     * Create requested Accounting period
27
     *
28
     * @param int $startYear first year to create
29
     * @param int $endYear   last yar to create - default is current year
30
     *
31
     * @return array Results
32
     */
33 1
    public function createYearsFrom($startYear, $endYear = null)
34
    {
35 1
        if (is_null($endYear)) {
36 1
            $endYear = date('Y');
37 1
        }
38
39 1
        for ($year = $startYear; $year <= $endYear; ++$year) {
40 1
            $obdobi = ['kod' => $year,
41 1
                'platiOdData' => $year.'-01-01T00:00:00',
42 1
                'platiDoData' => $year.'-12-31T23:59:59',
43 1
            ];
44 1
            $this->setData($obdobi);
45 1
            $result[] = $this->insertToFlexibee();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = 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...
46 1
            $this->addStatusMessage(print_r($result, 1));
0 ignored issues
show
Bug introduced by
The variable $result 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...
47 1
        }
48 1
        return $result;
49
    }
50
}
51