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

TablePrefixSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Fi\CoreBundle\Subscriber;
3
4
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
5
6
class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
7
{
8
9 36
    public function getSubscribedEvents()
10
    {
11 36
        return array('loadClassMetadata');
12
    }
13
14 24
    public function loadClassMetadata(LoadClassMetadataEventArgs $args)
15
    {
16 24
        $classMetadata = $args->getClassMetadata();
17 24
        if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
18
            // if we are in an inheritance hierarchy, only apply this once
19 24
            return;
20
        }
21 24
        if (false !== strpos($classMetadata->namespace, 'Fi\CoreBundle')) {
22 23
            $classMetadata->setPrimaryTable(array('name' => getenv("ficorebundle_table_prefix") . $classMetadata->getTableName()));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal ficorebundle_table_prefix 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...
23 23
            foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
24 21
                if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY &&
25 21
                    isset($classMetadata->associationMappings[$fieldName]['joinTable']['name'])
26
                ) {
27
                    $mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 53 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...
28 21
                    $classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
0 ignored issues
show
Bug Best Practice introduced by
The property prefix does not exist on Fi\CoreBundle\Subscriber\TablePrefixSubscriber. Did you maybe forget to declare it?
Loading history...
29
                }
30
            }
31
        }
32 24
    }
33
}
34