Completed
Push — master ( 41ad33...9fe0d2 )
by Sam
02:50
created

LabsHelper::getProjectMetadata()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.8981
c 0
b 0
f 0
cc 4
eloc 28
nc 6
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C LabsHelper::getTable() 4 26 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\HttpKernel\Exception\NotFoundHttpException;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * The Labs helper provides information relating to the WMF Labs installation of XTools.
14
 */
15
class LabsHelper
16
{
17
    /** @var string The current database name. */
18
    protected $dbName;
19
20
    /** @var Connection The database connection. */
21
    protected $client;
22
23
    /** @var ContainerInterface The DI container. */
24
    protected $container;
25
26
    /** @var string The project URL. */
27
    protected $url;
28
29
    /**
30
     * LabsHelper constructor.
31
     * @param ContainerInterface $container
32
     */
33
    public function __construct(ContainerInterface $container)
34
    {
35
        $this->container = $container;
36
    }
37
38
    /**
39
     * Check to see if a given tool is enabled.
40
     * @param string $tool The tool short name.
41
     * @return bool
42
     */
43
    public function checkEnabled($tool)
44
    {
45
        if (!$this->container->getParameter("enable.$tool")) {
46
            throw new NotFoundHttpException('This tool is disabled');
47
        }
48
    }
49
50
    /**
51
     * Is XTools connecting to WMF Labs?
52
     *
53
     * @return boolean
54
     */
55
    public function isLabs()
56
    {
57
        return (bool)$this->container->getParameter('app.is_labs');
58
    }
59
60
    /**
61
     * All mapping tables to environment-specific names, as specified in config/table_map.yml
62
     * Used for example to convert revision -> revision_replica
63
     * https://wikitech.wikimedia.org/wiki/Help:Tool_Labs/Database#Tables_for_revision_or_logging_queries_involving_user_names_and_IDs
64
     *
65
     * @param string $table  Table name
66
     * @param string $dbName Database name
67
     * @param string|null $table_extension Optional table extension, which will only get used if we're on labs.
68
     *
69
     * @return string Converted table name
70
     */
71
    public function getTable($table, $dbName = null, $table_extension = null)
72
    {
73
        // This is a workaround for a one-to-many mapping
74
        // as required by Labs.  We combine $table with
75
        // $table_extension in order to generate the new table name
76
        if ($this->isLabs() && $table_extension !== null) {
77
            $table = $table . "_" . $table_extension;
78
        }
79
80
        // Use the table specified in the table mapping configuration, if present.
81
        $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...
82 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...
83
            $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...
84
            $table = $this->container->getParameter("app.table.$table");
85
        }
86
87
        // Figure out database name.
88
        // Use class variable for the database name if not set via function parameter.
89
        $dbNameActual = $dbName ? $dbName : $this->dbName;
90
        if ($this->isLabs() && substr($dbNameActual, -2) != '_p') {
91
            // Append '_p' if this is labs.
92
            $dbNameActual .= '_p';
93
        }
94
95
        return $dbNameActual ? "$dbNameActual.$table" : $table;
96
    }
97
98
    // TODO: figure out how to use Doctrine to query host 'tools-db'
99
}
100