Completed
Push — 4.2 ( bbb68b...5e62fc )
by David
01:02
created

TDBMInheritanceException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 22
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A extendException() 0 14 4
1
<?php
2
3
namespace Mouf\Database\TDBM;
4
5
/**
6
 * Exception thrown when TDBM cannot find a straight path between supposedly inherited tables.
7
 */
8
class TDBMInheritanceException extends TDBMException
9
{
10
    public static function create(array $tables) : TDBMInheritanceException
11
    {
12
        return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables)));
13
    }
14
15
    public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData)
16
    {
17
        $pks = [];
18
        foreach ($beanData as $table => $row) {
19
            $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table);
20
            foreach ($primaryKeyColumns as $columnName) {
0 ignored issues
show
Bug introduced by
The expression $primaryKeyColumns of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
21
                if ($row[$columnName] !== null) {
22
                    $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true);
23
                }
24
            }
25
        }
26
27
        throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e);
28
    }
29
}
30