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

Handle visibility and docblocks of constants #32

Closed
wants to merge 4 commits into from
Closed

Handle visibility and docblocks of constants #32

wants to merge 4 commits into from

Conversation

simivar
Copy link
Contributor

@simivar simivar commented Jan 1, 2020

Q A
Bugfix no
BC Break yes
New Feature yes
RFC yes

Description

Currently library does not handle visibility of const fields in classes at all. All generated consts are defined as public. In this PR I've added third argument to addConstant method the same way that it is defined in addProperty method which User can use to set visibility of const.

Also, I've created new ConstantReflection that handles generating const from reflection with all data that it had in original class: visibility, dockblocks.

In order to keep it as BC I introduced FieldsReflectionTrait and FieldsReflectionInterface that is used and implemented by both ConstantReflection and PropertyReflection. I tried to come up with different, better solution as this one seems not good enough for me but couldn't. I am open to all suggestions and I will gladly make necessary changes.

Example 1

<?php

require_once 'vendor/autoload.php';

use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Reflection\ClassReflection;

class Test {
    /**
     * Description.
     *
     * @var string Second description.
     */
    protected const A = 'b';
};

$generator = ClassGenerator::fromReflection(
    new ClassReflection(Test::class)
);

$code = $generator->generate();
echo $code;

Before changes:

class Test
{

    public const A = 'b';


}

After changes:

class Test                                
{                                         
                                          
    /**                                   
     * Description.                       
     *                                    
     * @var string Second description.    
     */                                   
    protected const A = 'b';              
                                          
                                          
}

Example 2

use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\PropertyGenerator;

$foo = new ClassGenerator();
$foo->setName('Foo')
    ->addConstant('A', 'b', PropertyGenerator::FLAG_PROTECTED);

echo $foo->generate();

results in

class Foo
{

    protected const A = 'b';


}

weierophinney and others added 4 commits December 31, 2019 10:24
Thanks to new $flags it's possible to set visibility of constant introduced
in PHP 7.1. By default visibility is set to `public`.
Introduced ConstantReflection thanks to which when you generate class from
reflection it will handle things like visibility, docblocks, comments etc.
the same way that it handles it in PropertyReflection.
@michalbundyra
Copy link
Member

@simivar Just looked quickly and this is definitely BC Break. I don't think we can do it without and keep BC. Adding parameter to public method in non final class or changing parameter type hint are BC breaks.

@simivar
Copy link
Contributor Author

simivar commented Jan 2, 2020

@michalbundyra oh, yeah, you’re right. I got so caught up to keep it BC that I didn’t notice when I broke BC. I will think of some other way then using ‘trait’ to make that change. I am still open to all suggestions how you feel it should be done.

@geerteltink geerteltink changed the base branch from master to 3.5.x September 11, 2020 13:21
@geerteltink geerteltink changed the base branch from 3.5.x to 4.0.x September 11, 2020 13:21
@Ocramius Ocramius changed the base branch from 4.0.x to 4.2.x March 27, 2021 13:58
@Ocramius Ocramius changed the base branch from 4.2.x to 4.5.x December 7, 2021 06:01
@Ocramius
Copy link
Member

Ocramius commented Dec 7, 2021

Closing here: this patch is pre-Laminas rename, from what I can see, which means that the diff ended up being ginormous (due to foreign commits being in it).

The usage of a trait here is probably to be avoided, but I'd need to review a cleaned up patch, after evaluating that.

If interested in pursuing this further, I suggest re-opening a patch on top of 4.6.x, with just these commits in it:

@Ocramius Ocramius closed this Dec 7, 2021
@Ocramius Ocramius added the Invalid This doesn't seem right label Dec 7, 2021
@Ocramius Ocramius self-assigned this Dec 7, 2021
@alexander-schranz alexander-schranz mentioned this pull request Sep 11, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BC Break Invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants