Completed
Push — master ( 7c7cea...ce111c )
by Aleksandr
01:40
created

ChecksumBehavior::events()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace carono\checksum;
5
6
7
use yii\base\Behavior;
8
use yii\base\Widget;
9
use yii\helpers\Html;
10
use yii\widgets\ActiveForm;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, carono\checksum\ActiveForm.

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...
11
12
/**
13
 * Class ActiveFormBehavior
14
 *
15
 * @package carono\checksum
16
 * @property ActiveForm $owner
17
 */
18
class ChecksumBehavior extends Behavior
19
{
20
    public $_checksumInit = true;
21
22
    public function events()
23
    {
24
        return [
25
            Widget::EVENT_AFTER_RUN => 'registerChecksumField'
26
        ];
27
    }
28
29
    /**
30
     * @param \yii\base\WidgetEvent $event
31
     */
32
    public function registerChecksumField($event)
33
    {
34
        /**
35
         * @var Request $request
36
         */
37
        if (strtolower($this->owner->method) !== 'post' || (\Yii::$app->request instanceof Request && !\Yii::$app->request->checksumIsEnabled())) {
38
            return;
39
        }
40
        $request = \Yii::$app->request;
41
        $doc = new \DOMDocument();
42
        @$doc->loadHTML($event->result);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
        $xpath = new \DOMXpath($doc);
44
        $inputs = $xpath->query('//input | //select | //textarea');
45
        $result = [];
46
        for ($j = 0; $j < $inputs->length; $j++) {
47
            $input = $inputs->item($j);
48
            $result[] = $input->getAttribute('name');
49
        }
50
        $result = array_unique($result);
51
52
        parse_str(implode('&', $result), $stack);
53
        unset($doc, $xpath);
54
        $checksum = $request->setStack($stack);
55
        $input = Html::hiddenInput(\Yii::$app->request->checksumParam, $checksum);
56
        $event->result = str_replace('</form>', $input . '</form>', $event->result);
57
//            $stack = \Yii::$app->request->getStack($this->owner->id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
//            $key = Checksum::calculate($stack, \Yii::$app->request->checksumKey);
59
//            \Yii::$app->request->stackField($this->owner->id, \Yii::$app->request->checksumParam, $key);
60
//            echo Html::hiddenInput(\Yii::$app->request->checksumParam, $key);
61
    }
62
}