Completed
Push — travis-xenial ( 92e80e...c67dc4 )
by Kamil
05:33
created

SlugGenerationHelper::isElementNotReadonly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service;
15
16
use Behat\Mink\Driver\Selenium2Driver;
17
use Behat\Mink\Element\NodeElement;
18
use Behat\Mink\Session;
19
use DMore\ChromeDriver\ChromeDriver;
20
use Webmozart\Assert\Assert;
21
22
abstract class SlugGenerationHelper
0 ignored issues
show
Coding Style introduced by
SlugGenerationHelper does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
23
{
24
    public static function waitForSlugGeneration(Session $session, NodeElement $element): void
25
    {
26
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
27
28
        static::isElementReadonly($session, $element);
0 ignored issues
show
Bug introduced by
Since isElementReadonly() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isElementReadonly() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
29
30
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
31
    }
32
33
    public static function enableSlugModification(Session $session, NodeElement $element): void
34
    {
35
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
36
37
        static::isElementReadonly($session, $element);
0 ignored issues
show
Bug introduced by
Since isElementReadonly() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isElementReadonly() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
38
39
        $element->click();
40
41
        static::isElementNotReadonly($session, $element);
0 ignored issues
show
Bug introduced by
Since isElementNotReadonly() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isElementNotReadonly() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
42
43
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
44
    }
45
46
    public static function isSlugReadonly(Session $session, NodeElement $element): bool
47
    {
48
        if (!$session->getDriver() instanceof Selenium2Driver && !$session->getDriver() instanceof ChromeDriver) {
0 ignored issues
show
Bug introduced by
The class DMore\ChromeDriver\ChromeDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
49
            return $element->hasAttribute('readonly');
50
        }
51
52
        JQueryHelper::waitForAsynchronousActionsToFinish($session);
53
54
        return static::isElementReadonly($session, $element);
0 ignored issues
show
Bug introduced by
Since isElementReadonly() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isElementReadonly() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
55
    }
56
57
    private static function isElementReadonly(Session $session, NodeElement $element): bool
58
    {
59
        return $session->wait(1000, sprintf(
60
            'undefined != $(document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).attr("readonly")',
61
            str_replace('"', '\"', $element->getXpath())
62
        ));
63
    }
64
65
    private static function isElementNotReadonly(Session $session, NodeElement $element): bool
66
    {
67
        return $session->wait(1000, sprintf(
68
            'undefined == $(document.evaluate("%s", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue).attr("readonly")',
69
            str_replace('"', '\"', $element->getXpath())
70
        ));
71
    }
72
}
73