CreateIndexTask::run()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
rs 9.5555
cc 5
nc 12
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\FreeTextSearch\Task;
4
5
use League\CLImate\CLImate;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Dev\BuildTask;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Security\Security;
10
use Suilven\FreeTextSearch\Factory\IndexCreatorFactory;
11
12
class CreateIndexTask extends BuildTask
13
{
14
15
    protected $title = 'Create Index';
16
17
    protected $description = 'Create an index of a given name';
18
19
    protected $enabled = true;
20
21
    /** @var string */
22
    private static $segment = 'create-index';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
23
24
    /**
25
     * Implement this method in the task subclass to
26
     * execute via the TaskRunner
27
     *
28
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
29
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
30
     * @param \SilverStripe\Control\HTTPRequest $request
31
     * @return \SilverStripe\Control\HTTPResponse|void
32
     */
33
    public function run($request)
34
    {
35
        $climate = new CLImate();
36
37
        // check this script is being run by admin
38
        // @phpstan-ignore-next-line
39
        $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
40
41
        // for testing purposes
42
        $fail = $request->getVar('fail');
43
        if (!\is_null($fail)) {
44
            $canAccess = false;
45
        }
46
47
        if (!$canAccess) {
48
            return Security::permissionFailure(null, 'Permission denied');
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\Secu...l, 'Permission denied') returns the type SilverStripe\Control\HTTPResponse which is incompatible with the return type mandated by SilverStripe\Dev\BuildTask::run() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
49
        }
50
51
        $name = $request->getVar('index');
52
        $climate->info('NAME: ' . $name);
53
54
        $factory = new IndexCreatorFactory();
55
        $indexCreator = $factory->getIndexCreator();
56
57
        $indexCreator->createIndex($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $indexName of Suilven\FreeTextSearch\I...xCreator::createIndex() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $indexCreator->createIndex(/** @scrutinizer ignore-type */ $name);
Loading history...
58
    }
59
}
60