Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Handle nullable types #30

Closed

Conversation

camilledejoye
Copy link
Contributor

Go along with phpactor/worse-reflection#65

Fix some tests after the changes and handle types generation for completing the constructor and implementing contracts.

  • Add tests for PHP7.4 typed properties generation

After the change to the text-format indent method we no longer indent
blank lines.
So I had to fix the fixtures to avoir breaking tests
It removes a bug with two lines between the last property added and the
next method.
But it adds a blank line between the class's opening brace and the first
property when adding a property for the first time.
Still a good trade I would say, lot less common
Only the test here, the modifications were already done.
@camilledejoye
Copy link
Contributor Author

camilledejoye commented Dec 14, 2019

Is this mixing two PRs?

Are you only talking about the handling of templates paths ?

The there is the issue of the failing tests in the CI, we should merge worse-reflection then code-builder then code-transform for the CI to pass.

@dantleech
Copy link
Contributor

dantleech commented Dec 14, 2019

Are you only talking about the handling of templates paths ?

Yes - would rather keep these two PRs seperate, or am I missing something?

@dantleech
Copy link
Contributor

Lots of CS related fails - maybe we need to revert the changes to the example files?

@camilledejoye
Copy link
Contributor Author

camilledejoye commented Dec 14, 2019

Yes - would rather keep these two PRs seperate, or am I missing something?

I will do two PR in the code-builder package, I was working on it as the same time so I didn't stop to think about it.
But it should be possible to first handle the nullable types and after adding the override for templates

Lots of CS related fails - maybe we need to revert the changes to the example files?

CS ?
I see that phpunit fails but as I said it's because we miss the code-builder PR, because I have no errors on local.
I will quickly split the other PR in half so we can merge when ready

// We should not have to import it, we are extracting the
// method into the same file. We should not try to fix an
// oversight from the user, espacially since he might want
// to keep his partial qualified name
$builder->use((string) $variableType);
Copy link
Contributor

@dantleech dantleech Jan 18, 2020

Choose a reason for hiding this comment

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

we need to import it -- f.e.

  • a variable was retrieved from a method from another namespace
  • the variable has a type
  • it is now a parameter in the new method in the current namespace
  • we need to import the type.

Can we change the comment to reflect that?

We need to fix the whole qualfiied name issue, but it's a separate thing :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay to respond, I needed to take care of some other stuff and I don't have a lot of time left.

I'm not sure to see the case you're talking about.
Here we are extracting a method from a selection.
So the new method will always be created in the same scope: the class.
Meaning that any variable that we will have to provide as parameters to new method will already be imported in the scope.
If it is not, it's the fault of the user and we have no way of knowing which one to import in case there is multiple possibilities.

So I understand that we can try, just in case there is only one possibility.
But I don't think it's really SRP, here we should only build a method from the information at our disposal in the scope.
We should not modify the scope, even to fix an error.

If you still want to use the import I will change the comment for the one you proposed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Meaning that any variable that we will have to provide as parameters to new method will already be imported in the scope.

This is not true (I previously fell for this) -- if you call a method on an imported class, that method can return a type not in the current scope and you will therefore need to import it if it's a parameter of the extracted mehtod.

$parameterBuilder->type($variableType->short());
// TODO Create a utils class or something to get the shortest type by taking
// into account the import table, to handle aliases and partial namespaces
$shortTypeAsString = ($variableType->isNullable() ? '?' : '') . $variableType->short();
Copy link
Contributor

Choose a reason for hiding this comment

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

can we remove the TODO. This is to do with the qualified names topic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

@dantleech
Copy link
Contributor

this looks great -- can we create a utility class to format the type? or is it makes sense, make it part ofthe ->toString method of the Type clas?

@camilledejoye
Copy link
Contributor Author

this looks great -- can we create a utility class to format the type? or is it makes sense, make it part ofthe ->toString method of the Type clas?

It's a bit old in my head right now but I think we would need a separate service to achieve that.
Maybe even related to the qualified names topic.
Because the representation of a type actually depends of the context.
For example, phpdoc or PHP code ? We could take the position that we are dealing with PHP code mostly and it's not or responsibility to handle other representations than for PHP code.
In this case the __toString() method would fit our need, but there is another context to take into account: the scope.
Depending on it (current namespace & import table) the type would be represented differently.
And I don't think we should include any of that into the Type class.
A type remains the same no matter in which context we used it, only it's representation changes.

So I would go for a TypeFormater for example, which only responsibility would be to format the type properly.
We could have:

  • TypeFormater interface
  • PhpTypeFormater implementation
  • PhpdocTypeFormatter implementation

What do you think ?

@camilledejoye
Copy link
Contributor Author

After a year I think I'll close it to cleanup, might even be done in phpactor/code-builder#19 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants