removePluginTables()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ExtraField. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
6
$plugin = MigrationMoodlePlugin::create();
7
8
try {
9
    removeExtraField();
10
    removePluginTables();
11
12
    $plugin->uninstallHook();
13
} catch (Exception $exception) {
14
    $message = sprintf(
15
        $plugin->get_lang('UninstallError'),
16
        $exception->getMessage()
17
    );
18
19
    echo Display::return_message($message, 'error');
20
}
21
22
/**
23
 * @throws \Doctrine\ORM\ORMException
24
 * @throws \Doctrine\ORM\OptimisticLockException
25
 */
26
function removeExtraField()
27
{
28
    $em = Database::getManager();
29
30
    /** @var ExtraField $extraField */
31
    $extraField = $em
32
        ->getRepository('ChamiloCoreBundle:ExtraField')
33
        ->findOneBy(['variable' => 'moodle_password', 'extraFieldType' => ExtraField::USER_FIELD_TYPE]);
34
35
    if ($extraField) {
0 ignored issues
show
introduced by
$extraField is of type Chamilo\CoreBundle\Entity\ExtraField, thus it always evaluated to true.
Loading history...
36
        $em
37
            ->createQuery('DELETE FROM ChamiloCoreBundle:ExtraFieldValues efv WHERE efv.field = :field')
38
            ->execute(['field' => $extraField]);
39
40
        $em->remove($extraField);
41
        $em->flush();
42
    }
43
}
44
45
/**
46
 * Drop database table created by this plugin.
47
 */
48
function removePluginTables()
49
{
50
    $queries = [];
51
    $queries[] = "DROP TABLE IF EXISTS plugin_migrationmoodle_item";
52
    $queries[] = "DROP TABLE IF EXISTS plugin_migrationmoodle_task";
53
54
    foreach ($queries as $query) {
55
        Database::query($query);
56
    }
57
}
58