Passed
Push — develop ( 6fee9d...0ac4d1 )
by Andrea
41:39 queued 16:21
created

DatabaseUtils::truncateTable()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.5154

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 2
dl 0
loc 26
ccs 15
cts 22
cp 0.6818
crap 4.5154
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\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
use Doctrine\Common\Persistence\ObjectManager;
10
11
class DatabaseUtils
12
{
13
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
    private $kernel;
17
18 2
    public function __construct($kernel, ObjectManager $em)
19
    {
20 2
        $this->kernel = $kernel;
21 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...
22 2
    }
23
24 2
    public function getFieldType($entity, $field)
25
    {
26 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...
27 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...
28
29 2
        $fieldType = $fieldMetadata['type'];
30 2
        return $fieldType;
31
    }
32
33 1
    public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue)
34
    {
35 1
        $fieldtype = $this->getFieldType(new $entity(), $fieldname);
36 1
        if ($fieldtype === 'boolean') {
37 1
            return $oldvalue !== $newvalue;
38
        }
39 1
        if ($fieldtype === 'datetime' || $fieldtype === 'date') {
40 1
            return $this->isDateChanged($oldvalue, $newvalue);
41
        }
42 1
        if (is_array($oldvalue)) {
43 1
            return $this->isArrayChanged($oldvalue, $newvalue);
44
        }
45
46 1
        return ($oldvalue !== $newvalue);
47
    }
48
49 1
    public function isDateChanged($oldvalue, $newvalue)
50
    {
51 1
        $datenewvalue = new \DateTime();
52 1
        $datenewvalue->setTimestamp($newvalue);
53 1
        $twoboth = !$oldvalue && !$newvalue;
54 1
        if ($twoboth) {
55 1
            return false;
56
        }
57 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...
58 1
        if ($onlyonenull) {
59
            return true;
60
        }
61 1
        $changed = ($oldvalue != $datenewvalue);
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 1
        return count($numdiff) > 0;
77
    }
78
79 1
    public function truncateTable($entityclass, $cascade = false)
80
    {
81 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...
82 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

82
        /** @scrutinizer ignore-call */ 
83
        $connection = $this->em->getConnection();
Loading history...
83 1
        $dbPlatform = $connection->getDatabasePlatform();
84 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...
85 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...
86
87 1
        switch ($dbtype) {
88 1
            case 'mysql':
89 1
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
90 1
                $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...
91 1
                $retval = $connection->executeUpdate($q);
92 1
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
93 1
                break;
94
            case 'postgresql':
95
                $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...
96
                $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...
97
                break;
98
            default:
99
                $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...
100
                $retval = $connection->executeUpdate($q);
101
                break;
102
        }
103 1
        $this->em->clear();
104 1
        return $retval;
105
    }
106
107 2
    public function isSchemaChanged()
108
    {
109 2
        $application = new Application($this->kernel);
110 2
        $application->setAutoExit(false);
111
112 2
        $input = new ArrayInput(array(
113 2
            'command' => 'doctrine:schema:update',
114
            '--dump-sql' => true,
115
            '--no-debug' => true,
116 2
            '--env' => $this->kernel->getEnvironment(),
117
        ));
118
119
        // You can use NullOutput() if you don't need the output
120 2
        $output = new BufferedOutput();
121 2
        $application->run($input, $output);
122
123
        // return the output, don't use if you used NullOutput()
124 2
        $content = $output->fetch();
125 2
        $changed = strpos($content, 'Nothing to update');
126 2
        return ($changed !== 0);
127
    }
128
}
129