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

Fix return types in Query #115

Closed
wants to merge 1 commit into from
Closed

Conversation

szepeviktor
Copy link
Contributor

@szepeviktor szepeviktor commented Jul 14, 2021

Please consider running

vendor/bin/phpstan analyse -c vendor/szepeviktor/phpstan-wordpress/extension.neon src/ -l 5

@JJJ
Copy link
Collaborator

JJJ commented Jul 14, 2021

I don't think all of these are errors, but we should audit each of them regardless.

Results from current master branch:

 [ERROR] Found 86 errors

 ------ ----------------------------------------------------------------------------------------------
  Line   Database/Base.php
 ------ ----------------------------------------------------------------------------------------------
  14     Right side of || is always false.
  182    Parameter #1 $separator of function explode expects non-empty-string, string given.
  216    Method BerlinDB\Database\Base::sanitize_table_name() should return string but returns false.
  239    Method BerlinDB\Database\Base::sanitize_table_name() should return string but returns false.
 ------ ----------------------------------------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   Database/Column.php
 ------ -----------------------------------------------------------------------------------------------------------------------------
  14     Right side of || is always false.
  57     Property BerlinDB\Database\Column::$length (string) does not accept default value of type false.
  304    Property BerlinDB\Database\Column::$cache_key (string) does not accept default value of type false.
  673    Call to function is_null() with string will always evaluate to false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  673    Result of && is always false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  681    Elseif branch is unreachable because previous condition is always true.
  817    Parameter #1 $num of function number_format expects float, string|null given.
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------
  Line   Database/Queries/Compare.php
 ------ -------------------------------------------------------------
  14     Right side of || is always false.
  151    Comparison operation "<" between 1 and 0|1 is always false.
 ------ -------------------------------------------------------------

 ------ -----------------------------------------------------------------------------------------------------------------------------
  Line   Database/Queries/Date.php
 ------ -----------------------------------------------------------------------------------------------------------------------------
  14     Right side of || is always false.
  70     Property BerlinDB\Database\Queries\Date::$compare (array) does not accept default value of type string.
  78     Property BerlinDB\Database\Queries\Date::$start_of_week (array) does not accept default value of type int.
  249    Property BerlinDB\Database\Queries\Date::$now (int) does not accept string.
  251    Property BerlinDB\Database\Queries\Date::$compare (array) does not accept string.
  253    Property BerlinDB\Database\Queries\Date::$start_of_week (array) does not accept string.
  372    Method BerlinDB\Database\Queries\Date::get_now() should return string but returns int.
  447    Method BerlinDB\Database\Queries\Date::get_start_of_week() should return string but returns int.
  505    Binary operation "+" between string and 1 results in an error.
  659    Function apply_filters invoked with 3 parameters, 2 required.
  684    Function apply_filters invoked with 3 parameters, 2 required.
  776    Function apply_filters invoked with 5 parameters, 2 required.
  836    Parameter #2 $start_of_week of method BerlinDB\Database\Queries\Date::build_mysql_week() expects int, string given.
  838    Parameter #2 $start_of_week of method BerlinDB\Database\Queries\Date::build_mysql_week() expects int, string given.
  1086   Call to function is_int() with string will always evaluate to false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  1221   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1226   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1231   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1241   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1245   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1249   Parameter #2 $value of method BerlinDB\Database\Queries\Date::build_numeric_value() expects array|string|null, int given.
  1315   Function apply_filters invoked with 3 parameters, 2 required.
 ------ -----------------------------------------------------------------------------------------------------------------------------

 ------ -----------------------------------
  Line   Database/Queries/Meta.php
 ------ -----------------------------------
  14     Right side of || is always false.
 ------ -----------------------------------

 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   Database/Query.php
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------
  14     Right side of || is always false.
  188    Property BerlinDB\Database\Query::$meta_query (object) does not accept default value of type false.
  196    Property BerlinDB\Database\Query::$date_query (object) does not accept default value of type false.
  204    Property BerlinDB\Database\Query::$compare_query (object) does not accept default value of type false.
  363    Property BerlinDB\Database\Query::$last_changed (int) does not accept string.
  609    Call to function is_numeric() with array&nonEmpty will always evaluate to false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  609    Result of && is always false.
  819    Result of || is always false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  848    Method BerlinDB\Database\Query::get_items() never returns int so it can be removed from the return typehint.
  895    Property BerlinDB\Database\Query::$max_num_pages (int) does not accept float.
  918    Method BerlinDB\Database\Query::get_item_ids() never returns int so it can be removed from the return typehint.
  968    PHPDoc tag @param has invalid value (Query &$this  Current instance passed by reference.): Unexpected token "Current", expected variable at offset 162
  1280   Function apply_filters invoked with 4 parameters, 2 required.
  1280   PHPDoc tag @param has invalid value (object $this           The current Query instance.): Unexpected token "$this", expected variable at offset 241
  1347   Method BerlinDB\Database\Queries\Date::get_sql() invoked with 4 parameters, 0 required.
  1353   Cannot assign offset 'join' to string.
  1358   Cannot assign offset 'where' to string.
  1459   Method BerlinDB\Database\Query::parse_orderby() never returns false so it can be removed from the return typehint.
  1559   PHPDoc tag @param has invalid value (object &$this  Current instance of Query, passed by reference.): Unexpected token "Current", expected variable at offset
         185
  1730   Parameter #1 $items of method BerlinDB\Database\Query::update_item_cache() expects array, object|false given.
  1760   Parameter #2 $column_value of method BerlinDB\Database\Query::get_item_raw() expects string, int given.
  1827   Parameter #1 $items of method BerlinDB\Database\Query::update_item_cache() expects array, int given.
  1833   Method BerlinDB\Database\Query::add_item() should return bool but returns int.
  1851   Parameter #2 $column_value of method BerlinDB\Database\Query::get_item_raw() expects string, int given.
  1899   Parameter #2 $column_value of method BerlinDB\Database\Query::get_item_raw() expects string, int<min, -1>|int<1, max> given.
  1952   Parameter #1 $items of method BerlinDB\Database\Query::update_item_cache() expects array, int<min, -1>|int<1, max> given.
  1981   Parameter #2 $column_value of method BerlinDB\Database\Query::get_item_raw() expects string, int<min, -1>|int<1, max> given.
  2038   PHPDoc tag @param has invalid value (mixed ID of item, or row from database): Unexpected token "ID", expected variable at offset 150
  2202   PHPDoc tag @param references unknown parameter: $item
  2209   Method BerlinDB\Database\Query::transition_item() should return array but empty return statement found.
  2217   Method BerlinDB\Database\Query::transition_item() should return array but empty return statement found.
  2241   Method BerlinDB\Database\Query::transition_item() should return array but empty return statement found.
  2245   Method BerlinDB\Database\Query::transition_item() should return array but return statement is missing.
  2276   Default value of the parameter #4 $unique (false) of method BerlinDB\Database\Query::add_item_meta() is incompatible with type string.
  2293   Parameter #5 $unique of function add_metadata expects bool, string given.
  2368   Default value of the parameter #4 $delete_all (false) of method BerlinDB\Database\Query::delete_item_meta() is incompatible with type string.
  2385   Parameter #5 $delete_all of function delete_metadata expects bool, string given.
  2670   Method BerlinDB\Database\Query::prime_item_caches() should return bool but return statement is missing.
  2692   Call to function is_numeric() with array will always evaluate to false.
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting treatPhpDocTypesAsCertain: false in your
         vendor/szepeviktor/phpstan-wordpress/extension.neon.
  2777   Method BerlinDB\Database\Query::clean_item_cache() should return bool but return statement is missing.
  2798   Method BerlinDB\Database\Query::update_last_changed_cache() should return string but returns int.
  2846   Parameter #1 $key of method BerlinDB\Database\Query::cache_get() expects string, int given.
  3087   Parameter #2 $callback of function array_filter expects (callable(mixed, mixed): bool)|null, 'intval' given.
 ------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------------------------------------
  Line   Database/Row.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------
  14     Right side of || is always false.
  38     PHPDoc tag @param has invalid value (mixed Null by default, Array/Object if not): Unexpected token "Null", expected variable at offset 79
 ------ -------------------------------------------------------------------------------------------------------------------------------------------

 ------ -----------------------------------
  Line   Database/Schema.php
 ------ -----------------------------------
  14     Right side of || is always false.
 ------ -----------------------------------

 ------ -------------------------------------------------------------------------------------
  Line   Database/Table.php
 ------ -------------------------------------------------------------------------------------
  14     Right side of || is always false.
  367    Method BerlinDB\Database\Table::columns() should return array but returns false.
  770    Negated boolean expression is always false.
  778    Strict comparison using === between false and string will always evaluate to false.
 ------ -------------------------------------------------------------------------------------

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Jul 14, 2021

Yes, PHPStan gives you very good set of rules. It is for OOP only so it has some problems with procedural code.

I do not dare to comment on personal preferences but

defined( 'ABSPATH' ) || exit;

// should be properly written

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

Actually everything should be written properly :) meaning there are 10 ways a computer understands your code but PHPStan tells you which one to choose. I like it very much.

JJJ added a commit that referenced this pull request Aug 30, 2021
This commit improves the consistency of some docs and return values across all files.

See #115.
@JJJ
Copy link
Collaborator

JJJ commented Aug 31, 2021

Reduced from 86 to 20.

Gonna make more improvements incrementally.

Thanks @szepeviktor 🚀

@JJJ JJJ closed this Aug 31, 2021
@szepeviktor
Copy link
Contributor Author

You're welcome.

@szepeviktor szepeviktor deleted the patch-1 branch August 31, 2021 05:16
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.

2 participants