Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: CodeIgniter has context #5650

Merged
merged 9 commits into from
Feb 18, 2022
Merged

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Feb 3, 2022

Description
Supersede: #5649

  • add CodeIgniter::$context: web, php-cli, spark
  • make the constant SPARKED deprecated
  • remove is_cli() in CodeIgniter

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the refactor Pull requests that refactor code label Feb 3, 2022
@kenjis
Copy link
Member Author

kenjis commented Feb 3, 2022

@samsonasik Is this a bug of rector?

1 file with changes
===================

1) system/CodeIgniter.php:369

    ---------- begin diff ----------
@@ @@
     }

     /**
-     * Invoked via php-cli command?
-     */
-    private function isPhpCli(): bool
-    {
-        return $this->context === 'php-cli';
-    }
-
-    /**
      * Web access?
      */
     private function isWeb(): bool
    ----------- end diff -----------

Applied rules:
 * RemoveUnusedPrivateMethodRector

https://github.com/codeigniter4/CodeIgniter4/runs/5049699602?check_suite_focus=true

@samsonasik
Copy link
Member

Possibly, could you create failing test case? You can start with getrector.org/demo and click "Create a Test" after submit reproduced bug

@kenjis
Copy link
Member Author

kenjis commented Feb 4, 2022

@samsonasik

could you create failing test case?

No, I tried on getrector.org/demo, but RemoveUnusedPrivateMethodRector was not applied.

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this much better than your first attempt. Some comments. Also, I think we should set a default context on MockCodeIgniter (probably "web"?) so people's tests don't start failing unexpectedly.

spark Show resolved Hide resolved
system/CodeIgniter.php Show resolved Hide resolved
system/CodeIgniter.php Outdated Show resolved Hide resolved
system/CodeIgniter.php Show resolved Hide resolved
@kenjis
Copy link
Member Author

kenjis commented Feb 6, 2022

Also, I think we should set a default context on MockCodeIgniter (probably "web"?) so people's tests don't start failing unexpectedly.

Added.

system/CodeIgniter.php Outdated Show resolved Hide resolved
system/CodeIgniter.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member Author

kenjis commented Feb 6, 2022

@MGatner

Unless there is a compelling reason for these methods I prefer the simple property check inline.

The simple property check inline is too complex to me, as @samsonasik pointed out.
Can I use private methods?

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, still a few conversations going.

Comment on lines 18 to 25
/**
* Context
* web: Invoked by HTTP request
* php-cli: Invoked by CLI via `php public/index.php`
* spark: Invoked by CLI via the `spark` command
*
* @phpstan-var 'php-cli'|'spark'|'web'
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDEs will use the parent docblock so it's fine to drop this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped.

tests/system/ControllerTest.php Outdated Show resolved Hide resolved
@@ -65,6 +65,7 @@ protected function setUp(): void

$config = new App();
$this->codeigniter = new MockCodeIgniter($config);
$this->codeigniter->setContext('web');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are no longer needed with the default property?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@@ -59,6 +59,7 @@ protected function setUp(): void

$config = new App();
$this->codeigniter = new MockCodeIgniter($config);
$this->codeigniter->setContext('web');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are no longer needed with the default property?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@MGatner
Copy link
Member

MGatner commented Feb 6, 2022

The simple property check inline is too complex to me, as @samsonasik pointed out.

Can I use private methods?

I'm fine with the private methods. It still isn't my preference but I don't think either is "wrong" and I trust you as the author, unless there are other stronger opinions.

system/CodeIgniter.php Outdated Show resolved Hide resolved
@kenjis kenjis requested a review from samsonasik February 10, 2022 04:14
@kenjis
Copy link
Member Author

kenjis commented Feb 16, 2022

@MGatner Can you review?

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Should spark and public/index.php be included in the "All Changes" section?

@kenjis
Copy link
Member Author

kenjis commented Feb 18, 2022

Should spark and public/index.php be included in the "All Changes" section?

Strictly speaking no, I think. Because:

All changes noted here are optional until the next major version,
and any mandatory changes will be covered in the sections above.

@kenjis kenjis merged commit bc96617 into codeigniter4:develop Feb 18, 2022
@kenjis kenjis deleted the remove-SPARKED branch February 18, 2022 23:36
@samsonasik
Copy link
Member

@kenjis just tried a development build in a ci4 project:

php builds development
composer update
php spark serve

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-02-18 17:42:23 UTC-06:00

An uncaught Exception was encountered

Type:        Error
Message:     Typed property CodeIgniter\CodeIgniter::$context must not be accessed before initialization
Filename:    /Users/samsonasik/www/ci4-prj/vendor/codeigniter4/codeigniter4/system/CodeIgniter.php
Line Number: 308

        Backtrace:
                                                -48 - /Users/samsonasik/www/ci4-prj/vendor/codeigniter4/codeigniter4/system/CLI/Console.php::run
                                                                -57 - /Users/samsonasik/www/ci4-prj/spark::run

It seems the warning is not shown early for :

Context must be set before run() is called. If you are upgrading from 4.1.x, you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`.

as it not initialized.

@kenjis
Copy link
Member Author

kenjis commented Feb 18, 2022

@samsonasik
Can't reproduce.

$ php spark serve

CodeIgniter v4.1.8 Command Line Tool - Server Time: 2022-02-18 17:51:33 UTC-06:00

CodeIgniter development server started on http://localhost:8080
Press Control-C to stop.
[Sat Feb 19 08:51:33 2022] PHP 8.0.15 Development Server (http://localhost:8080) started

@samsonasik
Copy link
Member

@kenjis I created PR #5708 for it, the spark and public/index.php not yet updated, so the $context is accessed early, tested in php 8.1

@kenjis
Copy link
Member Author

kenjis commented Feb 18, 2022

@samsonasik I got it!

@kenjis kenjis mentioned this pull request Feb 21, 2022
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Pull requests that refactor code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants