Skip to content

feat: customize name of Shield Tables#628

Closed
datamweb wants to merge 15 commits intocodeigniter4:developfrom
datamweb:feat-rename-table-names
Closed

feat: customize name of Shield Tables#628
datamweb wants to merge 15 commits intocodeigniter4:developfrom
datamweb:feat-rename-table-names

Conversation

@datamweb
Copy link
Copy Markdown
Collaborator

@datamweb datamweb commented Feb 6, 2023

No description provided.

@datamweb datamweb added enhancement New feature or request docs needed Pull requests needing documentation write-ups and/or revisions. labels Feb 6, 2023
@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 6, 2023

What has happened PHPStan! There is no error in local!

Screenshot 2023-02-06 212606

And PHPUNIT test !

Screenshot 2023-02-06 213030

@datamweb datamweb force-pushed the feat-rename-table-names branch 3 times, most recently from 566dec8 to 52423f1 Compare February 6, 2023 20:29
@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 7, 2023

What has happened PHPStan! There is no error in local!

I also have no error on my local environment.
But config() returns object|null.
So null->authTables is "Trying to get property 'authTables' of non-object".

@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 7, 2023

@datamweb

And PHPUNIT test !

The test does not seem to pass on Windows from the beginning.

CLI::write() outputs PHP_EOL, but the test code uses LF as newline code.

@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 8, 2023

@kenjis , thanks for the explanation, it made sense.
But how can I fix error PHPStan?

@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 8, 2023

I thought it could be fixed.
I'm surprised, even when I tried to pass the check with @phpstan-ignore-line the error was still detected.

@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 8, 2023

Why?
PHP 8.0 and PHP 8.1

Error: Attempt to read property "tables" on null
 ------ ------------------------------------------- 
  Line   src/Config/Constants.php                   
 ------ ------------------------------------------- 
  10     Attempt to read property "tables" on null  
 ------ ------------------------------------------- 

@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 8, 2023

PHP 7.4:

Error: Trying to get property 'tables' of non-object
 ------ ----------------------------------------------- 
  Line   src/Config/Constants.php                       
 ------ ----------------------------------------------- 
  10     Trying to get property 'tables' of non-object  
 ------ -----------------------------------------------

@datamweb datamweb force-pushed the feat-rename-table-names branch 2 times, most recently from 03d4069 to 9dc0525 Compare February 9, 2023 19:24
@datamweb datamweb force-pushed the feat-rename-table-names branch from 479b840 to 07c85e1 Compare February 9, 2023 19:44
@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 9, 2023

@kenjis , after many efforts, I finally found the solution.
But I don't know exactly where the main problem is, I think it's in the framework , please let me know if you understand something.

@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 9, 2023

@datamweb I don't know what's wrong with PHPStan.
To begin with, I cannot reproduce the error.

But why don't we remove the constant?
I think using global constant is not good practice especially in libraries.
Global constants are for a user app, and a library should not consume them.

Ion Auth also havs $table, but it does not use a constant.
https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/6ebcd542011dbc35045466acad0172d2b68ae051/Config/IonAuth.php#L49-L54

We can get the values with config('Auth')->tables.

I gave it a try. See #633

@datamweb
Copy link
Copy Markdown
Collaborator Author

datamweb commented Feb 10, 2023

But why don't we remove the constant?

The reason I used constant was to add this feature with minimal changes.(
const
) vs
(wo_const)

Also, I think it is easier to read and review with this implementation. For example, look at PR commit, it's easier to suggest a change for SHIELD_TABLES['users'] or

    /**
     * Auth Table names
     */
    private array $tables;

    public function __construct(?Forge $forge = null)
    {
        parent::__construct($forge);

        /** @var Auth $authConfig */
        $authConfig   = config('Auth');
        $this->tables = $authConfig->tables;
    }
// and ...

In fact, I believe that using constant makes our codes shorter and more readable.
Also I was going to include a constant for the SHIELD_VERSION, which now I don't know where it fits with the no constant implementation.

Technically yes you are right, but we have an example of using constant in Bonfire2.

use CodeIgniter\Shield\Filters\SessionAuth;
use CodeIgniter\Shield\Filters\TokenAuth;

include_once __DIR__ . '/Constants.php';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't know we should use the constant or not.
But even if we use it, I would like to remove the include_once line and the constant file.

@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 11, 2023

If we add SHIELD_VERSION, we should add it as a class constant like CodeIgniter\CodeIgniter::CI_VERSION.
Class constants do not consume the global namespace.

@datamweb
Copy link
Copy Markdown
Collaborator Author

Okay, we will continue in your PR.

@kenjis
Copy link
Copy Markdown
Member

kenjis commented Feb 14, 2023

Closed by #633

@kenjis kenjis closed this Feb 14, 2023
@datamweb datamweb deleted the feat-rename-table-names branch February 14, 2023 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs needed Pull requests needing documentation write-ups and/or revisions. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants