Skip to content

[csm] strict typing && legacy code removal #1125

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

Merged
merged 1 commit into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Doctrine/EntityRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@ private function generateClass(ClassMetadata $metadata): string
private function createClassManipulator(string $classPath): ClassSourceManipulator
{
return new ClassSourceManipulator(
$this->fileManager->getFileContents($classPath),
$this->overwrite,
// use annotations
sourceCode: $this->fileManager->getFileContents($classPath),
overwrite: $this->overwrite,
// if properties need to be generated then, by definition,
// some non-annotation config is being used, and so, the
// properties should not have annotations added to them
false
useAttributesForDoctrineMapping: false
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Maker/MakeAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,10 @@ private function generateFormLoginFiles(string $controllerClass, string $userNam
throw new RuntimeCommandException(sprintf('Method "login" already exists on class %s', $controllerClassNameDetails->getFullName()));
}

$manipulator = new ClassSourceManipulator($controllerSourceCode, true);
$manipulator = new ClassSourceManipulator(
sourceCode: $controllerSourceCode,
overwrite: true
);

$this->securityControllerBuilder->addLoginMethod($manipulator);

Expand Down
6 changes: 5 additions & 1 deletion src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,11 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $

private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite): ClassSourceManipulator
{
$manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, false, true, true);
$manipulator = new ClassSourceManipulator(
sourceCode: $this->fileManager->getFileContents($path),
overwrite: $overwrite,
useAttributesForDoctrineMapping: true
);

$manipulator->setIo($io);

Expand Down
26 changes: 8 additions & 18 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
if ($this->addUniqueEntityConstraint) {
$classDetails = new ClassDetails($this->userClass);
$userManipulator = new ClassSourceManipulator(
file_get_contents($classDetails->getPath())
sourceCode: file_get_contents($classDetails->getPath())
);
$userManipulator->setIo($io);

Expand All @@ -382,34 +382,24 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
UniqueEntity::class,
['fields' => [$usernameField], 'message' => sprintf('There is already an account with this %s', $usernameField)]
);
} else {
$userManipulator->addAnnotationToClass(
UniqueEntity::class,
[
'fields' => [$usernameField],
'message' => sprintf('There is already an account with this %s', $usernameField),
]
);
}

$this->fileManager->dumpFile($classDetails->getPath(), $userManipulator->getSourceCode());
}

if ($this->willVerifyEmail) {
$classDetails = new ClassDetails($this->userClass);
$userManipulator = new ClassSourceManipulator(
file_get_contents($classDetails->getPath()),
false,
false,
true,
true
sourceCode: file_get_contents($classDetails->getPath()),
overwrite: false,
useAttributesForDoctrineMapping: true
);
$userManipulator->setIo($io);

$userManipulator->addProperty(
'isVerified',
['@ORM\Column(type="boolean")'],
false,
[$userManipulator->buildAttributeNode(Column::class, ['type' => 'boolean'], 'ORM')]
name: 'isVerified',
defaultValue: false,
attributes: [$userManipulator->buildAttributeNode(Column::class, ['type' => 'boolean'], 'ORM')]
);
$userManipulator->addAccessorMethod('isVerified', 'isVerified', 'bool', false);
$userManipulator->addSetter('isVerified', 'bool', false);
Expand Down
10 changes: 4 additions & 6 deletions src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,9 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
$useAttributesForDoctrineMapping = $this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($requestClassNameDetails->getFullName());

$manipulator = new ClassSourceManipulator(
$this->fileManager->getFileContents($requestEntityPath),
false,
!$useAttributesForDoctrineMapping,
true,
$useAttributesForDoctrineMapping
sourceCode: $this->fileManager->getFileContents($requestEntityPath),
overwrite: false,
useAttributesForDoctrineMapping: $useAttributesForDoctrineMapping
);

$manipulator->addInterface(ResetPasswordRequestInterface::class);
Expand Down Expand Up @@ -453,7 +451,7 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
);

$manipulator = new ClassSourceManipulator(
$this->fileManager->getFileContents($pathRequestRepository)
sourceCode: $this->fileManager->getFileContents($pathRequestRepository)
);

$manipulator->addInterface(ResetPasswordRequestRepositoryInterface::class);
Expand Down
9 changes: 4 additions & 5 deletions src/Maker/MakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
// need to write changes early so we can modify the contents below
$generator->writeChanges();

// @legacy @todo - refactor for better naming conventions BEFORE next release.
$useAttributesForDoctrineMapping = $userClassConfiguration->isEntity() && ($this->doctrineHelper->isDoctrineSupportingAttributes()) && $this->doctrineHelper->doesClassUsesAttributes($userClassNameDetails->getFullName());

// B) Implement UserInterface
$manipulator = new ClassSourceManipulator(
$this->fileManager->getFileContents($classPath),
true,
!$useAttributesForDoctrineMapping,
true,
$useAttributesForDoctrineMapping
sourceCode: $this->fileManager->getFileContents($classPath),
overwrite: true,
useAttributesForDoctrineMapping: $useAttributesForDoctrineMapping
Copy link
Member

Choose a reason for hiding this comment

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

Is this still valid? Like... when would $useAttributesForDoctrineMapping be false... XML? annotations? If annotations, should we throw an exception? If XML... should we throw an exception?

Copy link
Collaborator Author

@jrushlow jrushlow May 24, 2022

Choose a reason for hiding this comment

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

yes - well kinda, we still need to be able to generate user models if the class isn't an entity. I've added a legacy && todo notation a few lines above to fix the naming conventions in the pr #1128 im working on now.

);

$manipulator->setIo($io);
Expand Down
14 changes: 9 additions & 5 deletions src/Security/UserClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
);
} else {
// add normal property
$manipulator->addProperty($userClassConfig->getIdentityPropertyName());
$manipulator->addProperty(
name: $userClassConfig->getIdentityPropertyName()
);

$manipulator->addGetter(
$userClassConfig->getIdentityPropertyName(),
Expand Down Expand Up @@ -126,9 +128,8 @@ private function addGetRoles(ClassSourceManipulator $manipulator, UserClassConfi
} else {
// add normal property
$manipulator->addProperty(
'roles',
[],
new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT])
name: 'roles',
defaultValue: new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT])
);

$manipulator->addGetter(
Expand Down Expand Up @@ -230,7 +231,10 @@ private function addGetPassword(ClassSourceManipulator $manipulator, UserClassCo
);
} else {
// add normal property
$manipulator->addProperty('password', [$propertyDocs]);
$manipulator->addProperty(
name: 'password',
comments: [$propertyDocs]
);

$manipulator->addGetter(
'password',
Expand Down
6 changes: 5 additions & 1 deletion src/Test/MakerTestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public function deleteFile(string $filename): void
public function manipulateClass(string $filename, \Closure $callback): void
{
$contents = file_get_contents($this->getPath($filename));
$manipulator = new ClassSourceManipulator($contents, true, false, true, true);
$manipulator = new ClassSourceManipulator(
sourceCode: $contents,
overwrite: true,
useAttributesForDoctrineMapping: true
);
$callback($manipulator);

file_put_contents($this->getPath($filename), $manipulator->getSourceCode());
Expand Down
Loading