craftcms / rector

Rector rules for updating plugins and modules to Craft CMS 4.
19 stars 6 forks source link

Problem with main plugin class after running plugin #8

Closed clarknelson closed 2 years ago

clarknelson commented 2 years ago

Description

This is what I see when I run the script:

vendor/bin/rector process src --config vendor/craftcms/rector/sets/craft-cms-40.php

PHP Deprecated:  Return type of yii\db\QueryInterface::count($q = '*', $db = null) should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/clark/Documents/Websites/clarknelson.com/craft-recaptcha-3/vendor/yiisoft/yii2/db/QueryInterface.php on line 49
Deprecated: Return type of yii\db\QueryInterface::count($q = '*', $db = null) should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/clark/Documents/Websites/clarknelson.com/craft-recaptcha-3/vendor/yiisoft/yii2/db/QueryInterface.php on line 49
PHP Fatal error:  Type of clarknelson\craftrecaptcha3\CraftRecaptcha3::$schemaVersion must not be defined (as in class craft\base\Plugin) in /Users/clark/Documents/Websites/clarknelson.com/craft-recaptcha-3/src/CraftRecaptcha3.php on line 45
Fatal error: Type of clarknelson\craftrecaptcha3\CraftRecaptcha3::$schemaVersion must not be defined (as in class craft\base\Plugin) in /Users/clark/Documents/Websites/clarknelson.com/craft-recaptcha-3/src/CraftRecaptcha3.php on line 45

and my main plugin class:

<?php
/**
 * Craft reCAPTCHA 3 plugin for Craft CMS 3.x
 *
 * Verifies via Google the site and secret codes required to verify humanity through reCAPTCHA v3.
 *
 * @link      http://clarknelson.com
 * @copyright Copyright (c) 2019 Clark Nelson
 */

namespace clarknelson\craftrecaptcha3;

use clarknelson\craftrecaptcha3\models\Settings;
use clarknelson\craftrecaptcha3\twigextensions\DefaultTwigExtension;
use clarknelson\craftrecaptcha3\services\DefaultService;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\web\View;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterTemplateRootsEvent;

use yii\base\Event;

/**
 * Craft plugins are very much like little applications in and of themselves. We’ve made
 * it as simple as we can, but the training wheels are off. A little prior knowledge is
 * going to be required to write a plugin.
 *
 * For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
 * as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
 *
 * https://craftcms.com/docs/plugins/introduction
 *
 * @author    Clark Nelson
 * @package   CraftRecaptcha3
 * @since     1.0.0
 *
 * @property  Settings $settings
 * @method    Settings getSettings()
 */
class CraftRecaptcha3 extends Plugin
{
    // Static Properties
    // =========================================================================

    /**
     * Static property that is an instance of this plugin class so that it can be accessed via
     * CraftRecaptcha3::$plugin
     *
     * @var CraftRecaptcha3
     */
    public static $plugin;

    // Public Properties
    // =========================================================================

    /**
     * To execute your plugin’s migrations, you’ll need to increase its schema version.
     *
     * @var string
     */
    public string $schemaVersion = '1.0.0';

    // Public Methods
    // =========================================================================

    /**
     * Set our $plugin static property to this class so that it can be accessed via
     * CraftRecaptcha3::$plugin
     *
     * Called after the plugin class is instantiated; do any one-time initialization
     * here such as hooks and events.
     *
     * If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
     * you do not need to load it in your init() method.
     *
     */
    public function init()
    {
        parent::init();
        self::$plugin = $this;

        $this->setComponents([
            'captcha' => DefaultService::class
        ]);

        // Do something after we're installed
        // Default boilerplate code, we may need in the future though
        Event::on(
            Plugins::class,
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
            function (PluginEvent $event) {
                if ($event->plugin === $this) {
                    // We were just installed
                }
            }
        );

        // Register template directory for users to call
        Event::on(
            View::class,
            View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS,
            function (RegisterTemplateRootsEvent $event) {
                $event->roots['_recaptcha'] = __DIR__ . '/templates';
            }
        );

        if ($this->request->getIsSiteRequest()) {
            // Add in our Twig extension
            $this->view->registerTwigExtension(new DefaultTwigExtension());
        }
    }

    // Protected Methods
    // =========================================================================

    /**
     * Creates and returns the model used to store the plugin’s settings.
     *
     * @return \craft\base\Model|null
     */
    protected function createSettingsModel(): ?\craft\base\Model
    {
        return new Settings();
    }

    /**
     * Returns the rendered settings HTML, which will be inserted into the content
     * block on the settings page.
     *
     * @return string The rendered settings HTML
     */
    protected function settingsHtml(): ?string
    {
        return Craft::$app->view->renderTemplate(
            'craft-recaptcha-3/settings',
            [
                'settings' => $this->getSettings()
            ]
        );
    }
}

Any advice would be appreciated, I ran the scripts just how they are described in the documentation.

TomasVotruba commented 2 years ago

The error shows there cannot be type here:

-public string $schemaVersion = '1.0.0';
+public $schemaVersion = '1.0.0';

What is your version?

composer show craftcms/cms
clarknelson commented 2 years ago

Here's the output of that command:

name     : craftcms/cms
descrip. : Craft CMS
keywords : cms, craftcms, yii2
versions : * 3.7.43
type     : library
license  : proprietary
homepage : https://craftcms.com
source   : [git] https://github.com/craftcms/cms.git 7c58e9774db289725803c60f32b0d862a6e6e36c
dist     : [zip] https://api.github.com/repos/craftcms/cms/zipball/7c58e9774db289725803c60f32b0d862a6e6e36c 7c58e9774db289725803c60f32b0d862a6e6e36c
path     : /Users/clark/Documents/Websites/clarknelson.com/craft-recaptcha-3/vendor/craftcms/cms
names    : craftcms/cms, bower-asset/inputmask, bower-asset/jquery, bower-asset/punycode, bower-asset/yii2-pjax, yii2tech/ar-softdelete

support
docs : https://craftcms.com/docs/3.x/
email : support@craftcms.com
forum : https://craftcms.stackexchange.com/
issues : https://github.com/craftcms/cms/issues?state=open
rss : https://github.com/craftcms/cms/releases.atom
source : https://github.com/craftcms/cms

autoload
psr-4
craft\ => src/
yii2tech\ar\softdelete\ => lib/ar-softdelete/src/

requires
composer/composer 2.2.12
craftcms/oauth2-craftid ~1.0.0
craftcms/plugin-installer ~1.5.6
craftcms/server-check ~1.2.0
creocoder/yii2-nested-sets ~0.9.0
elvanto/litemoji ^3.0.1
enshrined/svg-sanitize ~0.15.2
ext-curl *
ext-dom *
ext-json *
ext-mbstring *
ext-openssl *
ext-pcre *
ext-pdo *
ext-zip *
guzzlehttp/guzzle ^6.5.5|^7.2.0
laminas/laminas-feed ~2.12.3|^2.13.1
league/flysystem ^1.1.4
league/oauth2-client ^2.6.0
mikehaertl/php-shellcommand ^1.6.3
php >=7.2.5
pixelandtonic/imagine ~1.2.4.1
seld/cli-prompt ^1.0.4
symfony/yaml ^5.2.1
true/punycode ^2.1.1
twig/twig ~2.14.3
voku/stringy ^6.4.0
webonyx/graphql-php ~14.11.5
yiisoft/yii2 ~2.0.45.0
yiisoft/yii2-debug ^2.1.16
yiisoft/yii2-queue ~2.3.2
yiisoft/yii2-swiftmailer ^2.1.2

requires (dev)
codeception/codeception ^4.0.0
codeception/module-asserts ^1.0.0
codeception/module-datafactory ^1.0.0
codeception/module-phpbrowser ^1.0.0
codeception/module-rest ^1.0.0
codeception/module-yii2 ^1.0.0
craftcms/ecs dev-main
fzaninotto/faker ^1.8
league/factory-muffin ^3.0
vlucas/phpdotenv ^3.0

suggests
ext-iconv Adds support for more character encodings than PHP’s built-in mb_convert_encoding() function, which Craft will take advantage of when converting strings to UTF-8.
ext-imagick Adds support for more image processing formats and options.
ext-intl Adds rich internationalization support.

provides
bower-asset/inputmask ~3.2.2 | ~3.3.5
bower-asset/jquery 3.5.*@stable | 3.4.*@stable | 3.3.*@stable | 3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable
bower-asset/punycode 1.3.*
bower-asset/yii2-pjax ~2.0.1
yii2tech/ar-softdelete 1.0.4

conflicts
league/oauth2-client 2.4.0
clarknelson commented 2 years ago

I removed the "string" type declaration, ran the script again, and it looks like it worked fine!

TomasVotruba commented 2 years ago

@clarknelson That's good news :+1: I wonder if we can fix it in an automated way here. Could you share the PR where you've run the Rector upgrade?

clarknelson commented 2 years ago

I've made a few commits to the plugin today on the master branch: https://github.com/clarknelson/craft-recaptcha-3

It seems like Craft 3 does not allow the "string" type declaration and Craft 4 requires the "string" type declaration, so I am going to release a 4.0.0 version for Craft 4 users. Craft 3 users can continue to use 1.2.2. Does this sound correct? It's a very simple plugin so I would like to have one version that supports both but I'm okay with the solution as listed.

TomasVotruba commented 2 years ago

I see. You've run Craft 4 upgrade set - https://github.com/craftcms/rector/tree/main/sets, but want to upgrade to Craft 3. In that case it's expected :)

Craft 4 requires the "string" type declaration, so I am going to release a 4.0.0 version for Craft 4 users

Sounds good :+1:

TomasVotruba commented 2 years ago

It's a very simple plugin so I would like to have one version that supports both but I'm okay with the solution as listed.

I don't think that's possible, as there are BC breaks. That's why it's mayor release. Yet new tags don't delete the new ones. So old users will simply use your version 3, that will still run on Craft 3.