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

Add built-in return types #5367

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Add built-in return types #5367

wants to merge 10 commits into from

Conversation

malberts
Copy link
Contributor

@malberts malberts commented Dec 22, 2022

Refs #5368

Done with Rector. This required multiple runs and some manual fixes where SMW was too ambiguous. My understanding is the more return types you have, the better it is able to infer additional missing types.

There are still cases where built-in types are missing, however in the current state Rector no longer adds anything for me, even after clearing Rector's cache. That might be due to ambiguity, or I suspect I need to use additional Rector rules, but I will try them in follow-up PRs.

The array return type refactoring adds comments documenting the array contents. I did not manually touch or review any of those additions.

This PR can be squashed. I did separate commits to make the reruns/manual fixes clearer for myself.


Rector config:

<?php

declare( strict_types=1 );

use Rector\Config\RectorConfig;
use Rector\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictConstantReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;

return static function ( RectorConfig $rectorConfig ): void {
	$rectorConfig->indent( "\t", 1);

	$rectorConfig->paths( [
		__DIR__ . '/includes',
		__DIR__ . '/src',
	] );

	$rectorConfig->skip( [
		//
		ReturnTypeFromStrictScalarReturnExprRector::class => [
			// getServiceLinkParams()
			__DIR__ . '/includes/datavalues/SMW_DataValue.php',
			// getUnit()
			__DIR__ . '/includes/datavalues/SMW_DV_Number.php',
		]
	] );

	$rectorConfig->rules( [
		ReturnTypeFromStrictScalarReturnExprRector::class,
		ReturnTypeFromStrictBoolReturnExprRector::class,
		ReturnTypeFromStrictConstantReturnRector::class,
		ReturnTypeFromStrictNativeCallRector::class,
		ReturnTypeFromStrictNewArrayRector::class,
		ReturnTypeFromStrictTypedCallRector::class,
		ReturnTypeFromStrictTypedPropertyRector::class,
	] );
};

@malberts malberts changed the title Add scalar return types Add built-in return types Dec 22, 2022
@malberts malberts marked this pull request as ready for review December 22, 2022 07:21
*/
public function getUnit(): string {
public function getUnit() {
Copy link
Contributor Author

@malberts malberts Dec 22, 2022

Choose a reason for hiding this comment

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

This is a manual fix where Rector didn't understand that a subclass (SMWQuantityValue) can return bool here.

I did not review the code to see if it's just a theorerical issue (i.e. perhaps it's bool initially but always changes to a string). The class member m_mainunit is typed as string|bool.

*/
protected function getServiceLinkParams(): bool {
protected function getServiceLinkParams() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a manual fix where Rector didn't understand that the subclasses return arrays.

@malberts malberts marked this pull request as draft December 22, 2022 07:33
@malberts
Copy link
Contributor Author

malberts commented Dec 22, 2022

Eh it messed up some comment indentations. Looks like you need to configure that first.

Fixed.

@@ -164,7 +164,10 @@ public function getColumnList( array $dataItems, $colsThreshold = 10 ) {
return $htmlColumns->getHtml();
}

private function buildList( $dataItems ) {
/**
* @return array<int|string, mixed[]>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an example of the automatically added array type. If this kind of documentation is pointless (albeit required for the CS tools) then I can rather remove these now. If somebody looks at this code later and comes up with a better type we can add it then.

Copy link
Member

Choose a reason for hiding this comment

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

This one does not seem super helpful. I'm also concerned correctness. If Rector is taking info from existing info in docs, then it might amplify mistakes in those docs. I do not trust type docs in SMW as I've seen them be wrong multiple times in the past.

@@ -47,7 +47,10 @@ public function get( $key ) {
return $this->{$key}( $this->store->getConnection( 'mw.db' ) );
}

private function loadFromDB( $connection ) {
/**
* @return array{total_row_count: mixed, last_id: mixed, duplicate_count: int, rows: array{rev_count: mixed, smw_namespace_group_by_count: mixed, smw_iw: array{delete_count: mixed, redirect_count: mixed}, smw_proptable_hash: array{query_match_count: mixed, query_null_count: mixed}}}[]|array{total_row_count: mixed, rows: array{active_links_count: mixed, invalid_links_count: mixed, unassigned_count: mixed}}[]|array{total_row_count: mixed, unique_terms_occurrence_in_percent: int|float, rows: array{blob_field_null_row_count: mixed, terms_occurrence: array{single_occurrence_total_count: mixed, multi_occurrence_total_count: mixed}}}[]|array{query_time: float, snapshot_date: string}[]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Spam?

Copy link
Member

Choose a reason for hiding this comment

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

Possibly yes

@@ -143,7 +143,10 @@ protected function doUpdateTable( $tableName, array $attributes = null ) {
}
}

private function getCurrentFields( $tableName ) {
/**
* @return string[]|string[][]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is wrong, but is strictly correct because $row->Type could be an array and str_replace can run on an array, hence why it thinks you can have 2D array.

@malberts malberts marked this pull request as ready for review December 22, 2022 09:08
@@ -759,7 +759,7 @@ public function getHash() {
*
* @return boolean
*/
public function isNumeric() {
public function isNumeric(): bool {
Copy link
Member

Choose a reason for hiding this comment

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

Return type narrowing to base classes or interfaces that are extended in other extensions require a major release. For this particular class, possibly Maps is the only affected extension, with its Geo DataValue.

Changes to ResultPrinter will have a wider impact

Copy link
Member

Choose a reason for hiding this comment

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

We can always make a pragmatic exception to the rule, but would need to asses the impact of doing so. Or we could bump to 5.x.

Either way, probably good to first do #5366 before merging potentially disruptive changes.

Copy link
Contributor Author

@malberts malberts Dec 22, 2022

Choose a reason for hiding this comment

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

What do you suggest? Should we aim for a new major version in the short term, or is adding types like this too much effort? Otherwise I'll rather focus on missing types throwing deprecation notices in PHP 8.1 (e.g. #5370). But I suppose there is still a chance that by adding a missing type there we might still cause narrowing. I'm not familiar with all the downstream extensions, so that makes it a bit difficult to test.

edit: I missed your second comment. Yes, doing #5366 first sounds like a good idea. If we end up bumping to 5.x we should probably also review the minimum PHP version.

Copy link
Member

Choose a reason for hiding this comment

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

We should definitely throw out 7.3, maybe also 7.4.

New major version in the short term seems fine to me, but would still do a patch/minor release first.

@malberts malberts marked this pull request as draft December 23, 2022 07:20
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

2 participants