Completed
Push — master ( 9fe0d2...0e0f1f )
by Sam
03:08
created

LabsHelper::checkEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * This file contains only the LabsHelper class.
4
 */
5
6
namespace AppBundle\Helper;
7
8
use Doctrine\DBAL\Connection;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * The Labs helper provides information relating to the WMF Labs installation of XTools.
13
 */
14
class LabsHelper
15
{
16
    /** @var string The current database name. */
17
    protected $dbName;
18
19
    /** @var Connection The database connection. */
20
    protected $client;
21
22
    /** @var ContainerInterface The DI container. */
23
    protected $container;
24
25
    /** @var string The project URL. */
26
    protected $url;
27
28
    /**
29
     * LabsHelper constructor.
30
     * @param ContainerInterface $container
31
     */
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $this->container = $container;
35
    }
36
37
    /**
38
     * Is XTools connecting to WMF Labs?
39
     *
40
     * @return boolean
41
     */
42
    public function isLabs()
43
    {
44
        return (bool)$this->container->getParameter('app.is_labs');
45
    }
46
47
    /**
48
     * All mapping tables to environment-specific names, as specified in config/table_map.yml
49
     * Used for example to convert revision -> revision_replica
50
     * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs
51
     *
52
     * @param string $table  Table name
53
     * @param string $dbName Database name
54
     * @param string|null $table_extension Optional table extension, which will only get used if we're on labs.
55
     *
56
     * @return string Converted table name
57
     */
58
    public function getTable($table, $dbName = null, $table_extension = null)
59
    {
60
        // This is a workaround for a one-to-many mapping
61
        // as required by Labs.  We combine $table with
62
        // $table_extension in order to generate the new table name
63
        if ($this->isLabs() && $table_extension !== null) {
64
            $table = $table . "_" . $table_extension;
65
        }
66
67
        // Use the table specified in the table mapping configuration, if present.
68
        $mapped = false;
0 ignored issues
show
Unused Code introduced by
$mapped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69 View Code Duplication
        if ($this->container->hasParameter("app.table.$table")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
            $mapped = true;
0 ignored issues
show
Unused Code introduced by
$mapped is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
            $table = $this->container->getParameter("app.table.$table");
72
        }
73
74
        // Figure out database name.
75
        // Use class variable for the database name if not set via function parameter.
76
        $dbNameActual = $dbName ? $dbName : $this->dbName;
77
        if ($this->isLabs() && substr($dbNameActual, -2) != '_p') {
78
            // Append '_p' if this is labs.
79
            $dbNameActual .= '_p';
80
        }
81
82
        return $dbNameActual ? "$dbNameActual.$table" : $table;
83
    }
84
85
    // TODO: figure out how to use Doctrine to query host 'tools-db'
86
}
87