Year::checkYear()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 1
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
ccs 9
cts 11
cp 0.8182
crap 4.0961
1
<?php
2
/**
3
 * Year
4
 *
5
 * @package Intraface_Accounting
6
 * @author  Lars Olesen
7
 * @since   1.0
8
 * @version     1.0
9
 */
10
require_once 'Intraface/modules/accounting/Account.php';
11
12
class Year extends Intraface_Standard
13
{
14
    public $id; // årsid
15
    public $kernel; // object
16
    public $value; // array
17
    public $error; // error object
18
19
    /**
20
     * Constructor
21
     *
22
     * @param object  $kernel
23
     * @param integer $year_id
24
     * @param boolean $load_active used when a new year is created
25
     *
26
     * @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...
27
     */
28 36
    function __construct($kernel, $year_id = 0, $load_active = true)
29
    {
30 36
        $this->error  = new Intraface_Error;
31 36
        $this->kernel = $kernel;
32 36
        $this->id     = (int)$year_id;
33
34 36
        if ($this->id > 0) {
35 1
            $this->load();
36 36
        } elseif ($load_active) {
37 36
            if ($this->loadActiveyear() > 0) {
38 19
                $this->load();
39 19
            }
40 36
        }
41 36
    }
42
43
    /**
44
     * Funktion til at sætte et regnskabsår, som brugeren redigerer i.
45
     *
46
     * @return true
47
     */
48 4
    function setYear()
49
    {
50 4
        if ($this->id == 0) {
51 1
            return false;
52
        }
53 3
        $this->reset();
54
55 3
        $this->kernel->getSetting()->set('user', 'accounting.active_year', $this->id);
56
57 3
        return true;
58
    }
59
60
    /**
61
     * Finder det aktive år.
62
     *
63
     * @todo should be deprecated in favor of getActiveYear
64
     *
65
     * @return year / false
66
     */
67 36
    public function loadActiveYear()
68
    {
69 36
        $this->id = $this->kernel->getSetting()->get('user', 'accounting.active_year');
70 36
        return $this->id;
71
    }
72
73
    /**
74
     * Finds the ative year
75
     *
76
     * @return integer
77
     */
78 3
    function getActiveYear()
79
    {
80 3
        return $this->kernel->getSetting()->get('user', 'accounting.active_year');
81
    }
82
83
    /**
84
     * Checks whether a year isset.
85
     *
86
     * @param boolean $redirect Set to true if a redirect should occur if no year isset
87
     *
88
     * @return boolean
89
     */
90 20
    function checkYear($redirect = true)
91
    {
92
        // hvis ikke der er sat noget aktivt år, skal det sættes
93 2
        $active_year = $this->getActiveYear();
94 2
        if (!$this->_isValid()) {
95 20
            $active_year = 0;
96 20
        }
97
98 2
        if (!$active_year) {
99 1
            if ($redirect) {
100
                header('Location: years.php');
101
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkYear() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
102
            }
103 1
            return false;
104
        }
105 1
        return true;
106
    }
107
108 1
    function isYearSet()
109
    {
110
        // if no active year isset, it has to be done
111 1
        $active_year = $this->loadActiveYear();
0 ignored issues
show
Unused Code introduced by
$active_year is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112 1
        if (!$this->_isValid()) {
113
            return false;
114
        }
115
116 1
        return true;
117
    }
118
119
    /**
120
     * Resets active year
121
     *
122
     * @return boolean
123
     */
124 3
    private function reset()
125
    {
126 3
        $this->kernel->getSetting()->set('user', 'accounting.active_year', 0);
127 3
        return true;
128
    }
129
130
    /*******************************************************************************
131
        OPDATERING OG LOAD
132
    *******************************************************************************/
133
134 30
    private function load()
135
    {
136 30
        if ($this->id == 0) {
137
            $this->value['id'] = 0;
138
            return;
139
        }
140
141
        $sql = "SELECT id,
142
                DATE_FORMAT(from_date, '%Y') AS year,
143
                DATE_FORMAT(from_date, '%d-%m-%Y') AS from_date_dk,
144
                DATE_FORMAT(to_date, '%d-%m-%Y') AS to_date_dk,
145
                last_year_id, from_date, to_date, locked, label, vat
146
            FROM accounting_year
147 30
            WHERE id = '" . $this->id . "'
148 30
                AND intranet_id = ".$this->kernel->intranet->get('id')."
149 30
            LIMIT 1";
150
151 30
        $db = new DB_Sql;
152 30
        $db->query($sql);
153
154 30
        if ($db->nextRecord()) {
155 30
            $this->id = $db->f('id');
156 30
            $this->value['id'] = $db->f('id');
157 30
            $this->value['year'] = $db->f('year');
158 30
            $this->value['label'] = $db->f('label');
159 30
            $this->value['last_year_id'] = $db->f('last_year_id');
160 30
            $this->value['from_date'] = $db->f('from_date');
161 30
            $this->value['from_date_dk'] = $db->f('from_date_dk');
162 30
            $this->value['to_date'] = $db->f('to_date');
163 30
            $this->value['to_date_dk'] = $db->f('to_date_dk');
164 30
            $this->value['locked'] = $db->f('locked');
165 30
            $this->value['vat'] = $db->f('vat');
166 30
        } else {
167 19
            $this->id = 0;
168 19
            $this->value['id'] = 0;
169
        }
170 30
    }
171
172 30
    private function validate(&$var)
173
    {
174 30
        $validator = new Intraface_Validator($this->error);
175
        // I could not find any use of the following, so i commented it out /SJ (22-01-2007)
176
        // $validator->isNumeric($var['year'], "year", "allow_empty");
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
177 30
        $validator->isNumeric($var['last_year_id'], "last_year_id", "allow_empty");
178 30
        $validator->isString($var['label'], "Du skal skrive et navn til året");
179 30
        $validator->isNumeric($var['locked'], "locked");
180 30
        settype($var['vat'], 'integer');
181 30
        $validator->isNumeric($var['vat'], "vat", 'allow_empty');
182
183 30
        if ($this->error->isError()) {
184
            return false;
185
        }
186 30
        return true;
187
    }
188
189
    /**
190
     * Updates year
191
     *
192
     * @param array $var Information about the year
193
     *
194
     * @return integer
195
     */
196 30
    public function save($var)
197
    {
198 30
        $var = safeToDb($var);
199
200 30
        $post_date_from = new Intraface_Date($var['from_date']);
201 30
        $post_date_from->convert2db();
202
203 30
        $post_date_to = new Intraface_Date($var['to_date']);
204 30
        $post_date_to->convert2db();
205
206 30
        if (!isset($var['last_year_id'])) {
207 19
            $var['last_year_id'] = 0;
208 19
        }
209
210 30
        if (!$this->validate($var)) {
211
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Year::save of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
212
        }
213
214 30
        if ($this->id > 0) {
215 2
            $sql="UPDATE accounting_year ";
216 2
            $sql_after=" WHERE id='".$this->id."' AND intranet_id = ".$this->kernel->intranet->get('id')."";
217 2
        } else {
218 30
            $sql="INSERT INTO accounting_year ";
219 30
            $sql_after = ', date_created = NOW()';
220
        }
221
        $sql.=" SET
222 30
            intranet_id='".$this->kernel->intranet->get('id')."',
223 30
            user_id='".$this->kernel->user->get('id')."',
224 30
            last_year_id='".$var['last_year_id']."',
225 30
            label='".$var['label']."',
226 30
            from_date='".$post_date_from->get()."',
227 30
            to_date = '".$post_date_to->get()."',
228 30
            locked='".$var['locked']."',
229
            date_changed = NOW(),
230 30
            vat = '".(int)$var['vat']."'
231 30
            $sql_after";
232
233 30
        $db = new DB_Sql;
234 30
        $db->query($sql);
235
236 30
        if ($this->id == 0) {
237 30
            $this->id = $db->insertedId();
238 30
        }
239
240 30
        $this->load();
241
242 30
        return $this->id;
243
    }
244
245
    /****************************************************************************
246
    VALIDERINGSFUNKTIONER
247
    ****************************************************************************/
248
    public function isValid()
249
    {
250
        return $this->_isValid();
251
    }
252
253
    /**
254
     * Checks whether year is valid
255
     *
256
     * @return 1 = year set; 0 = year NOT set
0 ignored issues
show
Documentation introduced by
The doc-type 1 could not be parsed: Unknown type name "1" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
257
     */
258 3 View Code Duplication
    private function _isValid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
259
    {
260
        $sql = "SELECT id FROM accounting_year
261 3
            WHERE id = ".$this->id."
262 3
                AND intranet_id = ". $this->kernel->intranet->get('id') . " AND active = 1";
263
264 3
        $db = new DB_Sql;
265 3
        $db->query($sql);
266
267
268 3
        if (!$db->nextRecord()) {
269
            return false;
270
        }
271
272 3
        return true;
273
    }
274
275 1
    function vatAccountIsSet()
276
    {
277 1
        if ($this->get('vat') == 0) {
278 1
            return true; // pretend it is set, when no vat on the year
279
        }
280
        if ($this->getSetting('vat_in_account_id') > 0 and $this->getSetting('vat_out_account_id') > 0 and $this->getSetting('vat_balance_account_id') > 0) {
0 ignored issues
show
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...
281
            return true;
282
        }
283
        return false;
284
    }
285
286
    /**
287
     * Checks whether the year is open for stating
288
     *
289
     * @return boolean
290
     */
291 10 View Code Duplication
    public function isYearOpen()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
292
    {
293 10
        $db = new Db_Sql;
294 10
        $db->query("SELECT locked FROM accounting_year WHERE id = " . $this->id . " AND intranet_id = ".$this->kernel->intranet->get('id'));
295 10
        if ($db->nextRecord()) {
296 10
            if ($db->f('locked') == 1) {
297
                return false;
298
            }
299 10
        }
300 10
        return true;
301
    }
302
303
    /**
304
     * is the date in the current year
305
     *
306
     * @param date $date Format: 0000-00-00
307
     *
308
     * @return boolean
309
     */
310 17
    public function isDateInYear($date)
311
    {
312 17
        if ($this->getId() == 0) {
313
            throw new Exception('Year has not been loaded yet - maybe not saved');
314
        }
315
316 17
        $date = safeToDb($date);
317
318 17
        $db = new Db_Sql;
319 17
        $db->query("SELECT from_date, to_date FROM accounting_year WHERE id= " . $this->id . " AND intranet_id = " . $this->kernel->intranet->get('id') . " LIMIT 1");
320 17
        if ($db->nextRecord()) {
321 17
            if ($db->f('from_date') <= $date and $date <= $db->f('to_date')) {
0 ignored issues
show
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...
322 15
                return true;
323
            }
324 2
        }
325 2
        return false;
326
    }
327
328
    /**
329
     * Checks whether the year is ready to use for stating
330
     *
331
     * @return boolean
332
     */
333 17
    public function readyForState($date = null)
334
    {
335 17
        if ($date === null) {
336 3
            $date = date('Y-m-d');
337 3
        }
338
339 17
        $return = true;
340
341 17
        if (!$this->get('id')) {
342 1
            $this->error->set('Der er ikke sat noget år.');
343 1
            $return = false;
344 17
        } elseif (!$this->isDateInYear($date)) {
345 1
            $this->error->set('Datoen er ikke i det år, der er sat i regnskabsmodulet.');
346 1
            $return = false;
347 16
        } elseif ($this->get('locked') == 1) {
348
            $this->error->set('Året er ikke åbent for bogføring.');
349
            $return = false;
350
        }
351
352 17
        return $return;
353
    }
354
355
    /**************************************************************************
356
        �VRIGE METODER
357
    **************************************************************************/
358
359
360
    /**
361
     * Gets a list
362
     *
363
     * @return array
364
     */
365 1
    function getList()
366
    {
367 1
        $gateway = new Intraface_modules_accounting_YearGateway($this->kernel);
368 1
        return $gateway->getList();
369
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
370
        if (!is_object($this->kernel)) {
371
            throw new Exception('Du kan ikke k�re Year::getList() uden at have instatieret klassen', FATAL);
372
        }
373
        $sql = "SELECT id, label FROM accounting_year
374
            WHERE intranet_id = ".$this->kernel->intranet->get('id')."
375
            ORDER BY from_date ASC";
376
377
        $db = new DB_Sql;
378
        $db->query($sql);
379
380
        if ($db->numRows() == 0) {
381
            return array();
382
        }
383
384
        while ($db->nextRecord()) {
385
            $account_years[$db->f("id")]['id'] = $db->f("id");
386
            $account_years[$db->f("id")]['label'] = $db->f("label");
387
        }
388
389
        return $account_years;
390
        */
391
    }
392
393 1
    function getBalanceAccounts()
394
    {
395
        // afstemningskonti
396 1
        $balance_accounts = unserialize($this->getSetting('balance_accounts'));
397
398 1
        if (!is_array($balance_accounts)) {
399
            throw new Exception('Balance accounts are not an array');
400
        }
401
402 1
        $sql_where = "";
403
404 1
        if (!empty($balance_accounts) and count($balance_accounts) > 0) {
0 ignored issues
show
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...
405
            foreach ($balance_accounts as $account) {
406
                $sql_where .= "id = " . $account . " OR ";
407
            }
408
        }
409
        // hvis der ikke er nogen balance_accounts skal den ikke v�lge nogen poster
410 1
        $sql_where .= "id=0";
411
412 1
        $db = new DB_Sql;
413 1
        $db->query("SELECT id FROM accounting_account
414 1
            WHERE (".$sql_where.")
415 1
            	AND intranet_id = " . $this->kernel->intranet->get('id') . "
416 1
            	AND year_id = " . $this->get('id'));
417
418 1
        $accounts = array(); // afstemningskonti
419 1
        $i = 0;
420 1
        while ($db->nextRecord()) {
421
            $oAccount = new Account($this, $db->f('id'));
422
            $oAccount->getSaldo('stated');
423
            $saldo = $oAccount->get('saldo');
424
            $oAccount->getSaldo('draft'); // f�r et array
425
426
            $accounts[$i]['id'] = $oAccount->get('id');
427
            $accounts[$i]['name'] = $oAccount->get('name');
428
            $accounts[$i]['number'] = $oAccount->get('number');
429
            $accounts[$i]['saldo_primo'] = $saldo;
430
            $accounts[$i]['saldo_draft'] = (float)$oAccount->get('saldo_draft');
431
            $accounts[$i]['saldo_ultimo'] = $saldo + $oAccount->get('saldo_draft');
432
            $i++;
433
        }
434 1
        return $accounts;
435
    }
436
437 21
    function setSetting($setting, $value)
438
    {
439 21
        return $this->kernel->getSetting()->set('intranet', 'accounting.'.$setting, $value, $this->get('id'));
440
    }
441
442 17
    function getSetting($setting)
443
    {
444 17
        return $this->kernel->getSetting()->get('intranet', 'accounting.' . $setting, $this->get('id'));
445
    }
446
447 20
    function createAccounts($type, $last_year_id = 0)
448
    {
449 20
        if ($this->getId() == 0) {
450
            throw new Exception('Year has no id');
451
        }
452
453 20
        $last_year_id = (int)$last_year_id;
454
        switch ($type) {
455 20
            case 'standard':
456 20
                    $standardaccounts = array();
457
                    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
458
                    // HACK
459
                    include(ROOT_PATH . 'intraface_modules/accounting/standardaccounts.php');
460
                    // HACK
461
462
                    if (empty($standardaccounts)) {
463
                        return 0;
464
                    }
465
                    */
466
467
                    // $module_accounting = $this->kernel->useModule('accounting');
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
468
                    // $module_accounting->includeFile('standardaccounts.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
469
                    // HACK
470
                    // include($module_accounting->includeFile('standardaccounts.php')); // 'intraface_modules/accounting/standardaccounts.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
471
                    // HACK
472
473 20
                    include('Intraface/modules/accounting/standardaccounts.php');
474
475 20
                if (empty($standardaccounts)) {
476
                    return false;
477
                }
478
479 20
                    $balance_accounts = array();
480 20
                    $buy_abroad = array();
481 20
                    $buy_eu = array();
482
483 20
                foreach ($standardaccounts as $input) {
484 20
                    require_once 'Intraface/modules/accounting/Account.php';
485 20
                    $account = new Account($this);
486 20
                    $input['vat_percent'] = $this->kernel->getSetting()->get('intranet', 'vatpercent');
487 20
                    $id = $account->save($input);
488
489
                    // settings
490 20
                    if (!empty($input['setting'])) {
491 20
                        $this->setSetting($input['setting'] . '_account_id', $id);
492 20
                    }
493 20
                    if (!empty($input['balance_account']) and $input['balance_account'] == 1) {
0 ignored issues
show
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...
494 20
                        $balance_accounts[] = $id;
495 20
                    }
496 20
                    if (!empty($input['result_account_id_start']) and $input['result_account_id_start']) {
0 ignored issues
show
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...
497 20
                        $this->setSetting('result_account_id_start', $id);
498 20
                    }
499
500 20
                    if (!empty($input['result_account_id_end']) and $input['result_account_id_end']) {
0 ignored issues
show
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...
501 20
                        $this->setSetting('result_account_id_end', $id);
502 20
                    }
503
504 20
                    if (!empty($input['balance_account_id_start']) and $input['balance_account_id_start']) {
0 ignored issues
show
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...
505 20
                        $this->setSetting('balance_account_id_start', $id);
506 20
                    }
507 20
                    if (!empty($input['capital_account']) and $input['capital_account']) {
0 ignored issues
show
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...
508 20
                        $this->setSetting('capital_account_id', $id);
509 20
                    }
510 20
                    if (!empty($input['balance_account_id_end']) and $input['balance_account_id_end']) {
0 ignored issues
show
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...
511 20
                        $this->setSetting('balance_account_id_end', $id);
512 20
                    }
513
514 20
                    if (!empty($input['buy_eu']) and $input['buy_eu'] == 1) {
0 ignored issues
show
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...
515 20
                        $buy_eu[] = $id;
516 20
                    }
517 20
                    if (!empty($input['buy_abroad']) and $input['buy_abroad'] == 1) {
0 ignored issues
show
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...
518 20
                        $buy_abroad[] = $id;
519 20
                    }
520 20
                }
521
522 20
                    $this->setSetting('balance_accounts', serialize($balance_accounts));
523 20
                    $this->setSetting('buy_abroad_accounts', serialize($buy_abroad));
524 20
                    $this->setSetting('buy_eu_accounts', serialize($buy_eu));
525
526 20
                break;
527
            case 'transfer_from_last_year':
528
                    // oprette konti
529
                if ($last_year_id == 0) {
530
                    return false;
531
                }
532
                    $last_year = new Year($this->kernel, $last_year_id);
533
                    $account = new Account($last_year);
534
                    $accounts = $account->getList();
535
536
                foreach ($accounts as $a) {
537
                    $old_account = new Account($last_year, $a['id']);
538
                    $input = $old_account->get();
539
                    $input['created_from_id'] = $old_account->get('id');
540
                    $new_account = new Account($this);
541
                    $new_account->save($input);
542
                }
543
                    // transfer setttings
544
                    // old account id will be connected to the new account
545
                if ($this->get('vat') > 0) {
546
                    $this->transferAccountSetting($last_year, 'vat_in_account_id');
547
                    $this->transferAccountSetting($last_year, 'vat_abroad_account_id');
548
                    $this->transferAccountSetting($last_year, 'vat_out_account_id');
549
                    $this->transferAccountSetting($last_year, 'vat_balance_account_id');
550
                    $this->transferAccountSetting($last_year, 'vat_free_account_id');
551
                    $this->transferAccountSetting($last_year, 'eu_sale_account_id');
552
                    //$this->transferAccountSetting($last_year, 'eu_buy_account_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
553
                    //$this->transferAccountSetting($last_year, 'abroad_buy_account_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
554
                }
555
                    $this->transferAccountSetting($last_year, 'result_account_id');
556
                    $this->transferAccountSetting($last_year, 'debtor_account_id');
557
                    $this->transferAccountSetting($last_year, 'credit_account_id');
558
559
                    $this->transferAccountSetting($last_year, 'result_account_id_start');
560
                    $this->transferAccountSetting($last_year, 'result_account_id_end');
561
                    $this->transferAccountSetting($last_year, 'balance_account_id_start');
562
                    $this->transferAccountSetting($last_year, 'balance_account_id_end');
563
                    $this->transferAccountSetting($last_year, 'capital_account_id');
564
565
                    $balance_accounts = unserialize($last_year->getSetting('balance_accounts'));
566
567
                    $db = new DB_Sql;
568
                    $new_balance_accounts = array();
569
570 View Code Duplication
                if (is_array($balance_accounts)) {
571
                    foreach ($balance_accounts as $key => $id) {
572
                        $db->query("SELECT id FROM accounting_account WHERE year_id = ".$this->get('id')." AND intranet_id = ".$this->kernel->intranet->get('id')." AND created_from_id = " . (int)$id);
573
                        while ($db->nextRecord()) {
574
                            $new_balance_accounts[] = $db->f('id');
575
                        }
576
                    }
577
                }
578
                    $this->setSetting('balance_accounts', serialize($new_balance_accounts));
579
580
                    $buy_eu_accounts = unserialize($last_year->getSetting('buy_eu_accounts'));
581
582
                    $db = new DB_Sql;
583
                    $new_buy_eu_accounts = array();
584
585 View Code Duplication
                if (is_array($buy_eu_accounts)) {
586
                    foreach ($buy_eu_accounts as $key => $id) {
587
                        $db->query("SELECT id FROM accounting_account WHERE year_id = ".$this->get('id')." AND intranet_id = ".$this->kernel->intranet->get('id')." AND created_from_id = " . (int)$id);
588
                        while ($db->nextRecord()) {
589
                            $new_buy_eu_accounts[] = $db->f('id');
590
                        }
591
                    }
592
                }
593
                    $this->setSetting('buy_eu_accounts', serialize($new_buy_eu_accounts));
594
595
                    $buy_abroad_accounts = unserialize($last_year->getSetting('buy_abroad_accounts'));
596
597
                    $db = new DB_Sql;
598
                    $new_buy_abroad_accounts = array();
599
600 View Code Duplication
                if (is_array($buy_abroad_accounts)) {
601
                    foreach ($buy_abroad_accounts as $key => $id) {
602
                        $db->query("SELECT id FROM accounting_account WHERE year_id = ".$this->get('id')." AND intranet_id = ".$this->kernel->intranet->get('id')." AND created_from_id = " . (int)$id);
603
                        while ($db->nextRecord()) {
604
                            $new_buy_abroad_accounts[] = $db->f('id');
605
                        }
606
                    }
607
                }
608
                    $this->setSetting('buy_abroad_accounts', serialize($new_buy_abroad_accounts));
609
610
611
                break;
612
            default:
613
                throw new Exception('A method to create the accounts must be chosen');
614
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
615
        }
616 20
        return true;
617
    }
618
619 1
    function setSettings($input)
620
    {
621 1
        if ($this->get('vat') > 0) {
622
            if (empty($input['vat_in_account_id'])) {
623
                $input['vat_in_account_id'] = 0;
624
            }
625
            $this->setSetting('vat_in_account_id', (int)$input['vat_in_account_id']);
626
627
            if (empty($input['vat_out_account_id'])) {
628
                $input['vat_out_account_id'] = 0;
629
            }
630
            $this->setSetting('vat_out_account_id', (int)$input['vat_out_account_id']);
631
632
            if (empty($input['vat_abroad_account_id'])) {
633
                $input['vat_abroad_account_id'] = 0;
634
            }
635
            $this->setSetting('vat_abroad_account_id', (int)$input['vat_abroad_account_id']);
636
637
            if (empty($input['vat_balance_account_id'])) {
638
                $input['vat_balance_account_id'] = 0;
639
            }
640
            $this->setSetting('vat_balance_account_id', (int)$input['vat_balance_account_id']);
641
642
            if (empty($input['vat_free_account_id'])) {
643
                $input['vat_free_account_id'] = 0;
644
            }
645
            $this->setSetting('vat_free_account_id', (int)$input['vat_free_account_id']);
646
647
            if (empty($input['eu_sale_account_id'])) {
648
                $input['eu_sale_account_id'] = 0;
649
            }
650
            $this->setSetting('eu_sale_account_id', (int)$input['eu_sale_account_id']);
651
            //$this->setSetting('eu_buy_account_id', (int)$input['eu_buy_account_id']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
652
            //$this->setSetting('abroad_buy_account_id', (int)$input['abroad_buy_account_id']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
653
        }
654 1
        if (empty($input['result_account_id'])) {
655 1
            $input['result_account_id'] = 0;
656 1
        }
657 1
        $this->setSetting('result_account_id', (int)$input['result_account_id']);
658
659 1
        if (empty($input['debtor_account_id'])) {
660 1
            $input['debtor_account_id'] = 0;
661 1
        }
662 1
        $this->setSetting('debtor_account_id', (int)$input['debtor_account_id']);
663
664 1
        if (empty($input['credit_account_id'])) {
665 1
            $input['credit_account_id'] = 0;
666 1
        }
667 1
        $this->setSetting('credit_account_id', (int)$input['credit_account_id']);
668
669 1
        if (empty($input['balance_accounts'])) {
670 1
            $input['balance_accounts'] = array();
671 1
        }
672 1
        $this->setSetting('balance_accounts', serialize($input['balance_accounts']));
673
674 1
        if (empty($input['buy_abroad_accounts'])) {
675 1
            $input['buy_abroad_accounts'] = array();
676 1
        }
677 1
        $this->setSetting('buy_abroad_accounts', serialize($input['buy_abroad_accounts']));
678
679 1
        if (empty($input['buy_eu_accounts'])) {
680 1
            $input['buy_eu_accounts'] = array();
681 1
        }
682 1
        $this->setSetting('buy_eu_accounts', serialize($input['buy_eu_accounts']));
683
684
685 1
        if (empty($input['result_account_id_start'])) {
686 1
            $input['result_account_id_start'] = 0;
687 1
        }
688 1
        $this->setSetting('result_account_id_start', $input['result_account_id_start']);
689
690 1
        if (empty($input['result_account_id_end'])) {
691 1
            $input['result_account_id_end'] = 0;
692 1
        }
693 1
        $this->setSetting('result_account_id_end', $input['result_account_id_end']);
694
695 1
        if (empty($input['balance_account_id_start'])) {
696 1
            $input['balance_account_id_start'] = 0;
697 1
        }
698 1
        $this->setSetting('balance_account_id_start', $input['balance_account_id_start']);
699
700 1
        if (empty($input['balance_account_id_end'])) {
701 1
            $input['balance_account_id_end'] = 0;
702 1
        }
703 1
        $this->setSetting('balance_account_id_end', $input['balance_account_id_end']);
704
705 1
        if (empty($input['capital_account_id'])) {
706 1
            $input['capital_account_id'] = 0;
707 1
        }
708 1
        $this->setSetting('capital_account_id', $input['capital_account_id']);
709
710 1
        return true;
711
    }
712
713 1
    function getSettings()
714
    {
715 1
        if ($this->get('vat') > 0) {
716
            $setting['vat_in_account_id'] = $this->getSetting('vat_in_account_id');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$setting was never initialized. Although not strictly required by PHP, it is generally a good practice to add $setting = 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...
717
            $setting['vat_out_account_id'] = $this->getSetting('vat_out_account_id');
718
            $setting['vat_abroad_account_id'] = $this->getSetting('vat_abroad_account_id');
719
            $setting['vat_balance_account_id'] = $this->getSetting('vat_balance_account_id');
720
            $setting['vat_free_account_id'] = $this->getSetting('vat_free_account_id');
721
            $setting['eu_sale_account_id'] = $this->getSetting('eu_sale_account_id');
722
            //$setting['eu_buy_account_id'] = $this->getSetting('eu_buy_account_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
723
            //$setting['abroad_buy_account_id'] = $this->getSetting('abroad_buy_account_id');
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
724
        }
725 1
        $setting['result_account_id'] = $this->getSetting('result_account_id');
0 ignored issues
show
Bug introduced by
The variable $setting 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...
726 1
        $setting['debtor_account_id'] = $this->getSetting('debtor_account_id');
727 1
        $setting['credit_account_id'] = $this->getSetting('credit_account_id');
728 1
        $setting['balance_accounts'] = unserialize($this->getSetting('balance_accounts'));
729 1
        $setting['buy_eu_accounts'] = unserialize($this->getSetting('buy_eu_accounts'));
730 1
        $setting['buy_abroad_accounts'] = unserialize($this->getSetting('buy_abroad_accounts'));
731
732 1
        $setting['result_account_id_start'] = $this->getSetting('result_account_id_start');
733 1
        $setting['result_account_id_end'] = $this->getSetting('result_account_id_end');
734 1
        $setting['balance_account_id_start'] = $this->getSetting('balance_account_id_start');
735 1
        $setting['balance_account_id_end'] = $this->getSetting('balance_account_id_end');
736
737 1
        $setting['capital_account_id'] = $this->getSetting('capital_account_id');
738
739 1
        return $setting;
740
    }
741
742
    /**
743
     * @param $from->year (object)
744
     * @param $setting (string)
745
     */
746
    function transferAccountSetting($from_year, $setting)
747
    {
748
        $account_id = $from_year->getSetting($setting);
749
        $db = new DB_Sql;
750
        $db->query("SELECT id FROM accounting_account WHERE year_id = ".$this->get('id')." AND intranet_id = ".$this->kernel->intranet->get('id')." AND created_from_id = " . $account_id);
751
        if ($db->nextRecord()) {
752
            $this->setSetting($setting, $db->f('id'));
753
        }
754
    }
755
756 1
    function isSettingsSet()
757
    {
758 1
        if (!$this->getSetting('result_account_id_start') or !$this->getSetting('result_account_id_end') or !$this->getSetting('balance_account_id_start') or !$this->getSetting('balance_account_id_end') or !$this->getSetting('capital_account_id')) {
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...
759 1
            return false;
760
        }
761
        return true;
762
    }
763
764
    /**
765
     *
766
     */
767 1
    function isBalanced()
768
    {
769 1
        $this->value['year_saldo'] = 0;
770
771 1
        $db = new DB_Sql;
772 1
        $db2 = new Db_Sql;
0 ignored issues
show
Unused Code introduced by
$db2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
773
774 1
        $db->query("SELECT distinct(account.id)
775
            FROM accounting_account account
776
            LEFT JOIN accounting_post post
777
                ON account.id = post.account_id
778
            WHERE
779 1
                account.intranet_id = ".$this->kernel->intranet->get('id')."
780
                AND account.active = 1
781 1
                AND account.year_id = ".$this->get('id')."
782 1
            ORDER BY account.number ASC");
783 1
        $i = 0;
784 1 View Code Duplication
        while ($db->nextRecord()) {
785
            $account = new Account($this, $db->f('id'));
786
787
            # ikke sum konti
788
            if ($account->get('type_key') == array_search('sum', $account->types)) {
789
                continue;
790
            }
791
792
            $account->getSaldo();
793
794
            $i++;
795
        }
796
797
        // HACK midlertidigt hack round() indtil alle beløb er integers i stedet for floats
798 1
        if (round($this->value['year_saldo']) == 0) {
799 1
            return true;
800
        } else {
801
            return false;
802
        }
803
    }
804
805
    /**************************************************************************
806
     * VALIDATE FUNCTIONS - IKKE EGENTLIG ÅRSRELATEREDE
807
     **************************************************************************/
808
809
    /**
810
     * Bruges i momsafregning og Årsafslutning
811
     *
812
     * @return boolean
813
     */
814
    function isStated($type, $date_start, $date_end)
815
    {
816
        if (!$this->kernel->user->hasModuleAccess('debtor')) {
817
            return true;
818
        }
819
820
        require_once 'Intraface/modules/debtor/Debtor.php';
821
        $types = Debtor::getDebtorTypes();
822
        $type_key = array_search($type, $types);
823
        if (empty($type_key)) {
824
            throw new Exception('Ugyldig type');
825
        }
826
        $db = new DB_Sql;
827
        $sql = "SELECT id FROM debtor
828
            WHERE type= " . $type_key . "
829
                AND intranet_id = " .$this->kernel->intranet->get('id') . "
830
                AND (this_date BETWEEN '" . $date_start . "' AND '" .$date_end . "')
831
                AND voucher_id = 0
832
                AND active = 1";
833
        $db->query($sql);
834
        if ($db->numRows() == 0) {
835
            return true;
836
        }
837
        return false;
838
    }
839
    
840
    function isPaymentsStated($date_start, $date_end)
841
    {
842
        $db = new DB_Sql;
843
        $sql = "SELECT id FROM invoice_payment
844
            WHERE intranet_id = " .$this->kernel->intranet->get('id') . "
845
                AND (payment_date BETWEEN '" . $date_start . "' AND '" .$date_end . "')
846
                AND voucher_id = 0";
847
        $db->query($sql);
848
        if ($db->numRows() == 0) {
849
            return true;
850
        }
851
        return false;
852
    }
853
    
854
    function isProcurementsStated($date_start, $date_end)
855
    {
856
        $db = new DB_Sql;
857
        $sql = "SELECT id FROM procurement
858
            WHERE intranet_id = " .$this->kernel->intranet->get('id') . "
859
                AND (invoice_date BETWEEN '" . $date_start . "' AND '" .$date_end . "')
860
                AND voucher_id = 0
861
                AND active = 1";
862
        $db->query($sql);
863
        if ($db->numRows() == 0) {
864
            return true;
865
        }
866
        return false;
867
    }
868
869
    function lock()
870
    {
871
        $db = new DB_Sql;
872
        $db->query("UPDATE accounting_year SET locked = 1 WHERE id = " . $this->id);
873
        return true;
874
    }
875
876 21
    public function getId()
877
    {
878 21
        return $this->get('id');
879
    }
880
}
881