Passed
Push — master ( 2a39ec...3b6c56 )
by Andrea
03:42
created

DatabaseUtility::isArrayChanged()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.0671

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
rs 8.8333
c 0
b 0
f 0
cc 7
nc 22
nop 2
crap 7.0671
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6
use Symfony\Bundle\FrameworkBundle\Console\Application;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
10
class DatabaseUtility
11
{
12
13
    private $container;
14
    /* @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...
15
    private $em;
16
17 2
    public function __construct(Container $container)
18
    {
19 2
        $this->container = $container;
20 2
        $this->em = $this->container->get("doctrine")->getManager();
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...
Coding Style Comprehensibility introduced by
The string literal doctrine does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
21 2
    }
22
23 2
    public function getFieldType($entity, $field)
24
    {
25 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...
26 2
        $fieldMetadata = $metadata->fieldMappings[$field];
27
28 2
        $fieldType = $fieldMetadata['type'];
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 ($fieldtype === 'boolean') {
36 1
            return $oldvalue !== $newvalue;
37
        }
38 1
        if ($fieldtype === 'datetime' || $fieldtype === 'date') {
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 1
            return true;
59
        }
60 1
        $changed = ($oldvalue != $datenewvalue);
61 1
        return $changed;
62
    }
63
64 1
    public function isArrayChanged($oldvalue, $newvalue)
65
    {
66 1
        $twoboth = !$oldvalue && !$newvalue;
67 1
        if ($twoboth) {
68
            return false;
69
        }
70 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...
71 1
        if ($onlyonenull) {
72 1
            return true;
73
        }
74 1
        $numdiff = array_diff($oldvalue, $newvalue);
75 1
        return count($numdiff) > 0;
76
    }
77
78 1
    public function truncateTable($entityclass, $cascade = false)
79
    {
80 1
        $cmd = $this->em->getClassMetadata($entityclass);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
81 1
        $connection = $this->em->getConnection();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
82 1
        $dbPlatform = $connection->getDatabasePlatform();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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
        $dbtype = $connection->getDriver()->getDatabasePlatform()->getName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
84 1
        $cascademysql = $cascade && $dbtype === 'mysql';
85 1
        if ($cascademysql) {
86
            $connection->query('SET FOREIGN_KEY_CHECKS=0');
87
        }
88 1
        $q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade);
89 1
        $connection->executeUpdate($q);
90 1
        if ($cascademysql) {
91
            $connection->query('SET FOREIGN_KEY_CHECKS=1');
92
        }
93 1
        $this->em->clear();
94 1
    }
95
96 2
    public function isSchemaChanged()
97
    {
98 2
        $kernel = $this->container->get("kernel");
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...
Coding Style Comprehensibility introduced by
The string literal kernel does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
99 2
        $application = new Application($kernel);
100 2
        $application->setAutoExit(false);
101
102 2
        $input = new ArrayInput(array(
103 2
            'command' => 'doctrine:schema:update',
104
            '--dump-sql' => true,
105
            '--no-debug' => true,
106 2
            '--env' => $kernel->getEnvironment(),
107
        ));
108
109
        // You can use NullOutput() if you don't need the output
110 2
        $output = new BufferedOutput();
111 2
        $application->run($input, $output);
112
113
        // return the output, don't use if you used NullOutput()
114 2
        $content = $output->fetch();
115 2
        $changed = strpos($content, 'Nothing to update');
116 2
        return ($changed!==0);
117
    }
118
}
119