Skip to content

Commit

Permalink
Merge pull request #8910 from jack-worman/Last_property_typehints
Browse files Browse the repository at this point in the history
Last property typehints
  • Loading branch information
orklah committed Dec 17, 2022
2 parents 2d9e3f4 + 9ce31a7 commit 62ced63
Show file tree
Hide file tree
Showing 44 changed files with 139 additions and 246 deletions.
4 changes: 1 addition & 3 deletions bin/generate_testsuites.php
Expand Up @@ -30,9 +30,7 @@
// -- xkcd:221

$order = array_map(
function (): int {
return mt_rand();
},
fn(): int => mt_rand(),
$files
);
array_multisort($order, $files);
Expand Down
8 changes: 2 additions & 6 deletions bin/update-property-map.php
Expand Up @@ -100,9 +100,7 @@ function extractClassesFromStatements(array $statements): array
$file,
get_class($exception),
$exception->getMessage(),
implode("\n", array_map(function (LibXMLError $error): string {
return $error->message;
}, libxml_get_errors()))
implode("\n", array_map(fn(LibXMLError $error): string => $error->message, libxml_get_errors()))
);
libxml_clear_errors();
continue;
Expand Down Expand Up @@ -159,9 +157,7 @@ function extractClassesFromStatements(array $statements): array

function serializeArray(array $array, string $prefix): string
{
uksort($array, function (string $first, string $second): int {
return strtolower($first) <=> strtolower($second);
});
uksort($array, fn(string $first, string $second): int => strtolower($first) <=> strtolower($second));
$result = "[\n";
$localPrefix = $prefix . ' ';
foreach ($array as $key => $value) {
Expand Down
58 changes: 25 additions & 33 deletions phpcs.xml
Expand Up @@ -10,7 +10,7 @@
* Configuration *
************************************************************************************************************** -->

<config name="php_version" value="70100"/>
<config name="php_version" value="70400"/>
<config name="installed_paths" value="../../slevomat/coding-standard"/>

<arg name="basepath" value="."/>
Expand Down Expand Up @@ -86,10 +86,10 @@
-->
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="declareOnFirstLine" type="bool" value="false"/>
<property name="linesCountBeforeDeclare" type="int" value="1"/>
<property name="linesCountAfterDeclare" type="int" value="1"/>
<property name="spacesCountAroundEqualsSign" type="int" value="0"/>
<property name="declareOnFirstLine" value="false"/>
<property name="linesCountBeforeDeclare" value="1"/>
<property name="linesCountAfterDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>

<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing"/>
Expand All @@ -113,8 +113,8 @@
-->
<rule ref="SlevomatCodingStandard.Namespaces.NamespaceSpacing">
<properties>
<property name="linesCountBeforeNamespace" type="int" value="1"/>
<property name="linesCountAfterNamespace" type="int" value="1"/>
<property name="linesCountBeforeNamespace" value="1"/>
<property name="linesCountAfterNamespace" value="1"/>
</properties>
</rule>

Expand All @@ -128,21 +128,21 @@
-->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFullyQualifiedExceptions" type="bool" value="false"/>
<property name="allowFullyQualifiedGlobalFunctions" type="bool" value="false"/>
<property name="allowFullyQualifiedGlobalConstants" type="bool" value="false"/>
<property name="allowFullyQualifiedGlobalClasses" type="bool" value="false"/>
<property name="allowFullyQualifiedExceptions" value="false"/>
<property name="allowFullyQualifiedGlobalFunctions" value="false"/>
<property name="allowFullyQualifiedGlobalConstants" value="false"/>
<property name="allowFullyQualifiedGlobalClasses" value="false"/>

<property name="allowFullyQualifiedNameForCollidingClasses" type="bool" value="false"/>
<property name="allowFullyQualifiedNameForCollidingFunctions" type="bool" value="false"/>
<property name="allowFullyQualifiedNameForCollidingConstants" type="bool" value="false"/>
<property name="allowFullyQualifiedNameForCollidingClasses" value="false"/>
<property name="allowFullyQualifiedNameForCollidingFunctions" value="false"/>
<property name="allowFullyQualifiedNameForCollidingConstants" value="false"/>

<property name="allowFallbackGlobalFunctions" type="bool" value="false"/>
<property name="allowFallbackGlobalConstants" type="bool" value="false"/>
<property name="allowFallbackGlobalFunctions" value="false"/>
<property name="allowFallbackGlobalConstants" value="false"/>

<property name="allowPartialUses" type="bool" value="true"/>
<property name="allowPartialUses" value="true"/>

<property name="searchAnnotations" type="bool" value="true"/>
<property name="searchAnnotations" value="true"/>
</properties>
</rule>

Expand All @@ -162,7 +162,7 @@
-->
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses">
<properties>
<property name="psr12Compatible" type="bool" value="true"/>
<property name="psr12Compatible" value="true"/>
<property name="caseSensitive" value="true"/>
</properties>
</rule>
Expand Down Expand Up @@ -228,16 +228,8 @@
-->
<rule ref="SlevomatCodingStandard.PHP.ShortList"/>

<!--
Requires arrow functions.
https://github.com/slevomat/coding-standard/#slevomatcodingstandardfunctionsrequirearrowfunction-
-->
<rule ref="SlevomatCodingStandard.Functions.RequireArrowFunction">
<properties>
<property name="enabled" value="true"/>
<property name="allowNested" value="true"/>
</properties>
</rule>
<!-- https://github.com/slevomat/coding-standard/blob/master/doc/functions.md#slevomatcodingstandardfunctionsrequirearrowfunction- -->
<rule ref="SlevomatCodingStandard.Functions.RequireArrowFunction"/>

<!--
Forces uniform arrow function style
Expand All @@ -259,9 +251,9 @@
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<!-- TODO: Go though these and fix all that can be fixed without breaking BC -->
<exclude-pattern>src/Psalm/Storage/Assertion/HasArrayKey\.php</exclude-pattern>
<exclude-pattern>src/Psalm/Storage/Assertion/IsNotCountable\.php</exclude-pattern>
<exclude-pattern>src/Psalm/Storage/Assertion/NonEmptyCountable\.php</exclude-pattern>
<!-- TODO: Remove these include-patterns in Psalm v6.0.0 -->
<include-pattern>bin/*</include-pattern>
<include-pattern>src/Psalm/Internal/*</include-pattern>
<include-pattern>tests/*</include-pattern>
</rule>
</ruleset>
9 changes: 3 additions & 6 deletions src/Psalm/CodeLocation.php
Expand Up @@ -74,20 +74,17 @@ class CodeLocation

private string $snippet = '';

/** @var null|string */
private $text;
private ?string $text = null;

/** @var int|null */
public $docblock_start;

/** @var int|null */
private $docblock_start_line_number;
private ?int $docblock_start_line_number = null;

/** @var int|null */
protected $docblock_line_number;

/** @var null|int */
private $regex_type;
private ?int $regex_type = null;

private bool $have_recalculated = false;

Expand Down
5 changes: 1 addition & 4 deletions src/Psalm/Config.php
Expand Up @@ -559,10 +559,7 @@ class Config
*/
public $debug_emitted_issues = false;

/**
* @var bool
*/
private $report_info = true;
private bool $report_info = true;

/**
* @var EventDispatcher
Expand Down
22 changes: 10 additions & 12 deletions src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
Expand Up @@ -265,19 +265,17 @@ public static function analyze(

$catch_context->vars_in_scope[$catch_var_id] = new Union(
array_map(
static function (string $fq_catch_class) use ($codebase): TNamedObject {
return new TNamedObject(
$fq_catch_class,
false,
false,
version_compare(PHP_VERSION, '7.0.0dev', '>=')
&& strtolower($fq_catch_class) !== 'throwable'
&& $codebase->interfaceExists($fq_catch_class)
&& !$codebase->interfaceExtends($fq_catch_class, 'Throwable')
static fn(string $fq_catch_class): TNamedObject => new TNamedObject(
$fq_catch_class,
false,
false,
version_compare(PHP_VERSION, '7.0.0dev', '>=')
&& strtolower($fq_catch_class) !== 'throwable'
&& $codebase->interfaceExists($fq_catch_class)
&& !$codebase->interfaceExtends($fq_catch_class, 'Throwable')
? ['Throwable' => new TNamedObject('Throwable')]
: []
);
},
: [],
),
$fq_catch_classes
)
);
Expand Down
Expand Up @@ -391,9 +391,9 @@ private function getDocblock(): string
$modified_docblock = true;
$inferredThrowsClause = array_reduce(
$this->throwsExceptions,
function (string $throwsClause, string $exception) {
return $throwsClause === '' ? $exception : $throwsClause.'|'.$exception;
},
fn(string $throwsClause, string $exception) => $throwsClause === ''
? $exception
: $throwsClause.'|'.$exception,
''
);
if (array_key_exists('throws', $parsed_docblock->tags)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Fork/ForkProcessErrorMessage.php
Expand Up @@ -11,7 +11,7 @@
class ForkProcessErrorMessage implements ForkMessage
{
use ImmutableNonCloneableTrait;

public string $message;

public function __construct(string $message)
Expand Down
18 changes: 8 additions & 10 deletions src/Psalm/Report/ByIssueLevelAndTypeReport.php
Expand Up @@ -17,8 +17,7 @@

final class ByIssueLevelAndTypeReport extends Report
{
/** @var string|null */
private $link_format;
private ?string $link_format = null;

public function create(): string
{
Expand Down Expand Up @@ -180,13 +179,12 @@ private function getFileReference($data): string

private function sortIssuesByLevelAndType(): void
{
usort($this->issues_data, function (IssueData $left, IssueData $right): int {
// negative error levels go to the top, followed by large positive levels, with level 1 at the bottom.
return [$left->error_level > 0, -$left->error_level, $left->type,
$left->file_path, $left->file_name, $left->line_from]
<=>
[$right->error_level > 0, -$right->error_level, $right->type,
$right->file_path, $right->file_name, $right->line_from];
});
usort(
$this->issues_data,
fn(IssueData $left, IssueData $right): int => [$left->error_level > 0, -$left->error_level,
$left->type, $left->file_path, $left->file_name, $left->line_from]
<=> [$right->error_level > 0, -$right->error_level, $right->type, $right->file_path, $right->file_name,
$right->line_from],
);
}
}
3 changes: 1 addition & 2 deletions src/Psalm/Report/ConsoleReport.php
Expand Up @@ -16,8 +16,7 @@

final class ConsoleReport extends Report
{
/** @var string|null */
private $link_format;
private ?string $link_format = null;

public function create(): string
{
Expand Down
18 changes: 7 additions & 11 deletions src/Psalm/Type/MutableUnion.php
Expand Up @@ -36,7 +36,7 @@ final class MutableUnion implements TypeNode, Stringable
/**
* @var non-empty-array<string, Atomic>
*/
private $types;
private array $types;

/**
* Whether the type originated in a docblock
Expand Down Expand Up @@ -158,22 +158,22 @@ final class MutableUnion implements TypeNode, Stringable
/**
* @var array<string, TLiteralString>
*/
private $literal_string_types = [];
private array $literal_string_types = [];

/**
* @var array<string, TClassString>
*/
private $typed_class_strings = [];
private array $typed_class_strings = [];

/**
* @var array<string, TLiteralInt>
*/
private $literal_int_types = [];
private array $literal_int_types = [];

/**
* @var array<string, TLiteralFloat>
*/
private $literal_float_types = [];
private array $literal_float_types = [];

/**
* True if the type was passed or returned by reference, or if the type refers to an object's
Expand Down Expand Up @@ -201,17 +201,13 @@ final class MutableUnion implements TypeNode, Stringable

/**
* This is a cache of getId on non-exact mode
*
* @var null|string
*/
private $id;
private ?string $id = null;

/**
* This is a cache of getId on exact mode
*
* @var null|string
*/
private $exact_id;
private ?string $exact_id = null;


/**
Expand Down
18 changes: 7 additions & 11 deletions src/Psalm/Type/Union.php
Expand Up @@ -49,7 +49,7 @@ final class Union implements TypeNode, Stringable
* @psalm-readonly
* @var non-empty-array<string, Atomic>
*/
private $types;
private array $types;

/**
* Whether the type originated in a docblock
Expand Down Expand Up @@ -170,22 +170,22 @@ final class Union implements TypeNode, Stringable
/**
* @var array<string, TLiteralString>
*/
private $literal_string_types = [];
private array $literal_string_types = [];

/**
* @var array<string, TClassString>
*/
private $typed_class_strings = [];
private array $typed_class_strings = [];

/**
* @var array<string, TLiteralInt>
*/
private $literal_int_types = [];
private array $literal_int_types = [];

/**
* @var array<string, TLiteralFloat>
*/
private $literal_float_types = [];
private array $literal_float_types = [];

/**
* True if the type was passed or returned by reference, or if the type refers to an object's
Expand Down Expand Up @@ -213,17 +213,13 @@ final class Union implements TypeNode, Stringable

/**
* This is a cache of getId on non-exact mode
*
* @var null|string
*/
private $id;
private ?string $id = null;

/**
* This is a cache of getId on exact mode
*
* @var null|string
*/
private $exact_id;
private ?string $exact_id;


/**
Expand Down
3 changes: 1 addition & 2 deletions tests/CodebaseTest.php
Expand Up @@ -24,8 +24,7 @@

class CodebaseTest extends TestCase
{
/** @var Codebase */
private $codebase;
private Codebase $codebase;

public function setUp(): void
{
Expand Down

0 comments on commit 62ced63

Please sign in to comment.