Completed
Push — 4.1 ( fe3b41...c2b28b )
by Andrea
12:59
created

DatabaseUtils::isDateChanged()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.0368

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
ccs 10
cts 11
cp 0.9091
rs 8.8333
c 0
b 0
f 0
cc 7
nc 22
nop 2
crap 7.0368
1
<?php
2
3
namespace Fi\CoreBundle\Utils\Database;
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 DatabaseUtils
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 1
    public function __construct(Container $container)
18
    {
19 1
        $this->container = $container;
20 1
        $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 1
    }
22
23 1
    public function getFieldType($entity, $field)
24
    {
25 1
        $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 1
        $fieldMetadata = $metadata->fieldMappings[$field];
27
28 1
        $fieldType = $fieldMetadata['type'];
29 1
        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
            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 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...
81 1
        $connection = $this->em->getConnection();
82 1
        $dbPlatform = $connection->getDatabasePlatform();
83 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...
84 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...
85
86 1
        switch ($dbtype) {
87 1
            case 'mysql':
88
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
89
                $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...
90
                $retval = $connection->executeUpdate($q);
91
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
92
                break;
93 1
            case 'postgresql':
94 1
                $cascadesql = $cascade ? "CASCADE" : "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal CASCADE 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...
Coding Style Comprehensibility introduced by
The string literal 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...
95 1
                $retval = $connection->executeQuery(sprintf('TRUNCATE TABLE %s ' . $cascadesql, $cmd->getTableName()));
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...
96 1
                break;
97
            default:
98
                $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...
99
                $retval = $connection->executeUpdate($q);
100
                break;
101
        }
102 1
        $this->em->clear();
103 1
        return $retval;
104
    }
105
106 1
    public function isSchemaChanged()
107
    {
108 1
        $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...
109 1
        $application = new Application($kernel);
110 1
        $application->setAutoExit(false);
111
112 1
        $input = new ArrayInput(array(
113 1
            'command' => 'doctrine:schema:update',
114
            '--dump-sql' => true,
115
            '--no-debug' => true,
116 1
            '--env' => $kernel->getEnvironment(),
117
        ));
118
119
        // You can use NullOutput() if you don't need the output
120 1
        $output = new BufferedOutput();
121 1
        $application->run($input, $output);
122
123
        // return the output, don't use if you used NullOutput()
124 1
        $content = $output->fetch();
125 1
        $changed = strpos($content, 'Nothing to update');
126 1
        return ($changed !== 0);
127
    }
128
}
129