Passed
Push — master ( 2df7c7...f817d3 )
by Andrea
18:46
created

DatabaseUtils::isArrayChanged()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.0671

Importance

Changes 0
Metric Value
cc 7
eloc 8
nc 22
nop 2
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 7.0671
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Database;
4
5
use Symfony\Bundle\FrameworkBundle\Console\Application;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Doctrine\Common\Persistence\ObjectManager;
9
10
class DatabaseUtils
11
{
12
    /* @var $em \Doctrine\ORM\EntityManager */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $em at position 0 could not be parsed: Unknown type name '$em' at position 0 in $em.
Loading history...
13
    private $em;
14
    private $kernel;
15
16 2
    public function __construct($kernel, ObjectManager $em)
17
    {
18 2
        $this->kernel = $kernel;
19 2
        $this->em = $em;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20 2
    }
21
22 2
    public function getFieldType($entity, $field)
23
    {
24 2
        $metadata = $this->em->getClassMetadata(get_class($entity));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
25 2
        $fieldMetadata = $metadata->fieldMappings[$field];
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
27 2
        $fieldType = $fieldMetadata['type'];
28
29 2
        return $fieldType;
30
    }
31
32 1
    public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue)
33
    {
34 1
        $fieldtype = $this->getFieldType(new $entity(), $fieldname);
35 1
        if ('boolean' === $fieldtype) {
36 1
            return $oldvalue !== $newvalue;
37
        }
38 1
        if ('datetime' === $fieldtype || 'date' === $fieldtype) {
39 1
            return $this->isDateChanged($oldvalue, $newvalue);
40
        }
41 1
        if (is_array($oldvalue)) {
42 1
            return $this->isArrayChanged($oldvalue, $newvalue);
43
        }
44
45 1
        return $oldvalue !== $newvalue;
46
    }
47
48 1
    public function isDateChanged($oldvalue, $newvalue)
49
    {
50 1
        $datenewvalue = new \DateTime();
51 1
        $datenewvalue->setTimestamp($newvalue);
52 1
        $twoboth = !$oldvalue && !$newvalue;
53 1
        if ($twoboth) {
54 1
            return false;
55
        }
56 1
        $onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue);
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $onlyonenull = (! $oldva...ldvalue && ! $newvalue), Probably Intended Meaning: $onlyonenull = ! $oldval...ldvalue && ! $newvalue)
Loading history...
57 1
        if ($onlyonenull) {
58
            return true;
59
        }
60 1
        $changed = ($oldvalue != $datenewvalue);
61
62 1
        return $changed;
63
    }
64
65 1
    public function isArrayChanged($oldvalue, $newvalue)
66
    {
67 1
        $twoboth = !$oldvalue && !$newvalue;
68 1
        if ($twoboth) {
69
            return false;
70
        }
71 1
        $onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue);
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: $onlyonenull = (! $oldva...ldvalue && ! $newvalue), Probably Intended Meaning: $onlyonenull = ! $oldval...ldvalue && ! $newvalue)
Loading history...
72 1
        if ($onlyonenull) {
73 1
            return true;
74
        }
75 1
        $numdiff = array_diff($oldvalue, $newvalue);
76
77 1
        return count($numdiff) > 0;
78
    }
79
80 1
    public function truncateTable($entityclass, $cascade = false)
81
    {
82 1
        $cmd = $this->em->getClassMetadata($entityclass);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
83 1
        $connection = $this->em->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

83
        /** @scrutinizer ignore-call */ 
84
        $connection = $this->em->getConnection();
Loading history...
84 1
        $dbPlatform = $connection->getDatabasePlatform();
85 1
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
86 1
        $retval = false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
87
88 1
        switch ($dbtype) {
89 1
            case 'mysql':
90
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
91
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
92
                $retval = $connection->executeUpdate($q);
93
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
94
                break;
95 1
            case 'postgresql':
96 1
                $cascadesql = $cascade ? 'CASCADE' : '';
97 1
                $tablename = $cmd->getTableName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
98 1
                if ($cmd->getSchemaName()) {
99 1
                    $tablename = $cmd->getSchemaName().'.'.$tablename;
100
                }
101 1
                $retval = $connection->executeQuery(sprintf('TRUNCATE TABLE %s '.$cascadesql, $tablename));
102 1
                break;
103
            default:
104
                $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
                $retval = $connection->executeUpdate($q);
106
                break;
107
        }
108 1
        $this->em->clear();
109
110 1
        return $retval;
111
    }
112
113 2
    public function isSchemaChanged()
114
    {
115 2
        $application = new Application($this->kernel);
116 2
        $application->setAutoExit(false);
117
118 2
        $input = new ArrayInput(array(
119 2
            'command' => 'doctrine:schema:update',
120
            '--dump-sql' => true,
121
            '--no-debug' => true,
122 2
            '--env' => $this->kernel->getEnvironment(),
123
        ));
124
125
        // You can use NullOutput() if you don't need the output
126 2
        $output = new BufferedOutput();
127 2
        $application->run($input, $output);
128
129
        // return the output, don't use if you used NullOutput()
130 2
        $content = $output->fetch();
131 2
        $changed = strpos($content, 'Nothing to update');
132
133 2
        return 0 !== $changed;
134
    }
135
}
136