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

NEW Use composer 2 #62

Merged

Conversation

emteknetnz
Copy link
Collaborator

@emteknetnz emteknetnz commented Apr 20, 2022

Issue silverstripe/silverstripe-framework#10250

Switch from composer 1 to composer 2, also add the ?? operator to a few native function calls to ensure null values aren't passed in for PHP 8.1 compatibility

Create 3 + 3.0 branches and tag 3.0.0 when merged

Required for silverstripe/recipe-reporting-tools#26

@emteknetnz
Copy link
Collaborator Author

@spekulatius are you OK with us tagging a new major (v3) to support composer v2? It's required for PHP 8.1 support.

@emteknetnz emteknetnz marked this pull request as ready for review April 20, 2022 05:07
@@ -113,7 +101,7 @@ public function onAfterBuild()
// Mock COMPOSER_HOME if it's not defined already. Composer requires one of the two to be set.
if (!Environment::getEnv('COMPOSER_HOME')) {
$home = Environment::getEnv('HOME');
if (!$home || !is_dir($home) || !is_writable($home)) {
if (!$home || !is_dir($home ?? '') || !is_writable($home ?? '')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

!$home should short circuit on null, so the null coalescing operator isn't necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated

@@ -53,7 +51,7 @@ public function getPackages(array $allowedTypes = null)
$repository = $this->getRepository();
foreach ($repository->getPackages() as $package) {
// Filter out packages that are not "allowed types"
if (is_array($allowedTypes) && !in_array($package->getType(), $allowedTypes)) {
if (is_array($allowedTypes) && !in_array($package->getType(), $allowedTypes ?? [])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if is_array($allowedTypes) is false, then we won't get to the in_array() call, so the null coalescing operator isn't necessary.

@@ -130,7 +118,7 @@ public function onAfterBuild()
$this->setComposer($composer);

if ($originalDir !== BASE_PATH) {
chdir($originalDir);
chdir($originalDir ?? '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

$originalDir is either a string or false, never null.

Suggested change
chdir($originalDir ?? '');
chdir($originalDir ?: '');

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted

@@ -117,7 +116,7 @@ protected function findLatestPackage(
}

$targetVersion = null;
if (0 === strpos($installedVersion, 'dev-')) {
if (0 === strpos($installedVersion ?? '', 'dev-')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Noting here for posterity:
$installedVersion comes from PackageInterface::getPrettyVersion(). This is strongly typed from 2.3.0 to return a string - but not in 2.0.0.

*/
protected function getInstalledConstraint(BaseRepository $repository, $packageName)
protected function getInstalledConstraint(InstalledRepository $repository, string $packageName): string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Link::getPrettyConstraint() can return null in composer 2.0.0

Suggested change
protected function getInstalledConstraint(InstalledRepository $repository, string $packageName): string
protected function getInstalledConstraint(InstalledRepository $repository, string $packageName)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've added $link->getPrettyConstraint() ?? '' so that it's always a string

*
* @param BaseRepository $repository
* @param string $packageName
* @return string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we removing the PHPDoc typings here (and, I've just noticed, elsewhere as well)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because this is going to be a new major, and I've switched to using proper method type hints

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the intention that anywhere we have type hints we should remove phpdoc typings? I'm generally in favour of that wherever the phpdoc doesn't provide additional context such as here - but in that case we should probably do it as a blanket action everywhere, yes?
I think we should probably leave the phpdocs alone for this PR, since making graphql4 strongly typed is already another load of work that needs to be done, and it should be done consistently (either leaving/updating them or removing them).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

everywhere, yes?

No. Most of Silverstripe was made for PHP 5, is dynamically typed and relys on docblocks for typehinting.

There are plenty of example where there are strongly typed methods with docblocks, though that was done when PHP 5.6 was still supported. It is no longer supported. However removing the old docblocks on a case by case basis right now is very low priority. I'm sure we'll do an automated bulk cleanup at some point in the future.

Where new API is created (which is what is happening here, this is for a new major after all), I'm using strong typing and not using docblock types

Copy link
Collaborator

Choose a reason for hiding this comment

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

I had meant "everywhere" as in "everywhere in this module where new API is provided" so I think we're on the same page there.

Also for some reason when I made that comment my brain was telling me this was on silverstripe/graphql which it definitely isn't. 😅Sorry about that. Yup I'm okay with this.

Copy link
Collaborator

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

Ran locally, worked great. For any private repositories, a short line is added to the messages for the queued job as below, and that repository doesn't have its "latest" or "available" columns populated in the report.

[DEBUG] Could not authenticate against github.com

LGTM. I don't have access to merge this, though.

@emteknetnz
Copy link
Collaborator Author

@spekulatius Just to let you know we're going to merge this and tag a new major, let us know if this is an issue. If we haven't heard from you within a few days we'll assume this is ok and go ahead with it.

@spekulatius
Copy link
Member

Sounds good to me @emteknetnz

@emteknetnz emteknetnz merged commit 416a550 into bringyourownideas:master Apr 26, 2022
@emteknetnz emteknetnz deleted the pulls/master/composer2 branch April 26, 2022 01:16
emteknetnz added a commit that referenced this pull request Apr 26, 2022
emteknetnz added a commit that referenced this pull request Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants