Issues (45)

src/Socializer.php (2 issues)

1
<?php
2
/**
3
 * Socializer plugin for Craft CMS 3.x
4
 *
5
 * @dedicado Al amor de mi vida, mi Sara **).
6
 * @link      https://enupal.com/
7
 * @copyright Copyright (c) 2019 Enupal LLC
8
 */
9
10
namespace enupal\socializer;
11
12
use Craft;
13
use craft\base\Plugin;
14
15
use craft\events\RegisterUrlRulesEvent;
16
use craft\web\twig\variables\CraftVariable;
17
use craft\web\UrlManager;
18
use enupal\socializer\models\Settings;
19
use enupal\socializer\services\App;
20
use enupal\socializer\variables\SocializerVariable;
21
use yii\base\Event;
22
use craft\services\Gc;
23
24
class Socializer extends Plugin
25
{
26
    /**
27
     * Enable use of Socializer::$app-> in place of Craft::$app->
28
     *
29
     * @var App
30
     */
31
    public static $app;
32
33
    /**
34
     * @var string
35
     */
36
    public string $schemaVersion = '2.0.0';
37
38
    public bool $hasCpSection = true;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public bool $hasCpSettings = true;
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function init()
49
    {
50
        parent::init();
51
        self::$app = $this->get('app');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->get('app') can also be of type mixed. However, the property $app is declared as type enupal\socializer\services\App. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
52
53
        Event::on(
54
            CraftVariable::class,
55
            CraftVariable::EVENT_INIT,
56
            function(Event $event) {
57
                /** @var CraftVariable $variable */
58
                $variable = $event->sender;
59
                $variable->set('socializer', SocializerVariable::class);
60
            }
61
        );
62
63
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
64
            $event->rules = array_merge($event->rules, $this->getCpUrlRules());
65
        }
66
        );
67
68
        Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) {
69
            $event->rules = array_merge($event->rules, $this->getSiteUrlRules());
70
        }
71
        );
72
73
        Event::on(Gc::class, Gc::EVENT_RUN, function() {
74
            Craft::$app->gc->hardDelete('{{%enupalsocializer_providers}}');
0 ignored issues
show
The method hardDelete() does not exist on null. ( Ignorable by Annotation )

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

74
            Craft::$app->gc->/** @scrutinizer ignore-call */ 
75
                             hardDelete('{{%enupalsocializer_providers}}');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
        });
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    private function getCpUrlRules()
82
    {
83
        return [
84
            'enupal-socializer/providers/new' =>
85
                'enupal-socializer/providers/edit-provider',
86
87
            'enupal-socializer/providers/edit/<providerId:\d+>' =>
88
                'enupal-socializer/providers/edit-provider'
89
        ];
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function getCpNavItem(): ?array
96
    {
97
        $parent = parent::getCpNavItem();
98
        return array_merge($parent, [
99
            'subnav' => [
100
                'providers' => [
101
                    "label" => Craft::t('enupal-socializer',"Providers"),
102
                    "url" => 'enupal-socializer/providers'
103
                ],
104
                'settings' => [
105
                    "label" => Craft::t('enupal-socializer',"Settings"),
106
                    "url" => 'enupal-socializer/settings'
107
                ]
108
            ]
109
        ]);
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    private function getSiteUrlRules()
116
    {
117
        return [
118
            'socializer/login' =>
119
                'enupal-socializer/login/login',
120
121
            'socializer/login/callback' =>
122
                'enupal-socializer/login/callback'
123
        ];
124
    }
125
126
    protected function settingsHtml(): ?string
127
    {
128
        return Craft::$app->getView()->renderTemplate('enupal-socializer/settings/index');
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    protected function createSettingsModel(): ?\craft\base\Model
135
    {
136
        return new Settings();
137
    }
138
}
139