Skip to content

Commit

Permalink
Return type hints (#7065)
Browse files Browse the repository at this point in the history
Co-authored-by: ralila <>
  • Loading branch information
rarila committed Dec 5, 2021
1 parent f67a0ca commit 39402c2
Show file tree
Hide file tree
Showing 346 changed files with 889 additions and 854 deletions.
26 changes: 26 additions & 0 deletions phpcs.xml
Expand Up @@ -10,6 +10,7 @@
* Configuration *
************************************************************************************************************** -->

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

<arg name="basepath" value="."/>
Expand Down Expand Up @@ -126,4 +127,29 @@
</properties>
</rule>

<!--
Checks for missing return typehints and useless @return annotations.
https://github.com/slevomat/coding-standard#slevomatcodingstandardtypehintsreturntypehint-
-->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<properties>
<property name="enableObjectTypeHint" type="bool" value="false"/>
<property name="enableStaticTypeHint" type="bool" value="false"/>
<property name="enableMixedTypeHint" type="bool" value="false"/>
<property name="enableUnionTypeHint" type="bool" value="false"/>
</properties>

<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
</rule>

<!--
Enforces consistent formatting of return typehints.
https://github.com/slevomat/coding-standard#slevomatcodingstandardtypehintsreturntypehintspacing-
-->
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing">
<properties>
<property name="spacesCountBeforeColon" value="0"/>
</properties>
</rule>

</ruleset>
2 changes: 1 addition & 1 deletion src/Psalm/CodeLocation.php
Expand Up @@ -384,7 +384,7 @@ public function getHash(): string
return $this->file_name . ' ' . $this->raw_file_start . $this->raw_file_end;
}

public function getShortSummary() : string
public function getShortSummary(): string
{
return $this->file_name . ':' . $this->getLineNumber() . ':' . $this->getColumn();
}
Expand Down
16 changes: 7 additions & 9 deletions src/Psalm/Codebase.php
Expand Up @@ -560,7 +560,7 @@ public function exhumeClassLikeStorage(string $fq_classlike_name, string $file_p
}
}

public static function getPsalmTypeFromReflection(?ReflectionType $type) : Type\Union
public static function getPsalmTypeFromReflection(?ReflectionType $type): Type\Union
{
return Reflection::getPsalmTypeFromReflectionType($type);
}
Expand Down Expand Up @@ -823,8 +823,6 @@ public function getFunctionLikeStorage(
*
* @param string|MethodIdentifier $method_id
* @param string|MethodIdentifier|null $calling_method_id
*
@return bool
*/
public function methodExists(
$method_id,
Expand Down Expand Up @@ -1513,7 +1511,7 @@ public function getTypeContextAtPosition(string $file_path, Position $position):
/**
* @return list<CompletionItem>
*/
public function getCompletionItemsForClassishThing(string $type_string, string $gap) : array
public function getCompletionItemsForClassishThing(string $type_string, string $gap): array
{
$completion_items = [];

Expand Down Expand Up @@ -1595,7 +1593,7 @@ public function getCompletionItemsForPartialSymbol(
string $type_string,
int $offset,
string $file_path
) : array {
): array {
$fq_suggestion = false;

if (($type_string[1] ?? '') === '\\') {
Expand Down Expand Up @@ -1818,7 +1816,7 @@ public function getCompletionItemsForType(Type\Union $type): array
*/
public function getCompletionItemsForArrayKeys(
string $type_string
) : array {
): array {
$completion_items = [];
$type = Type::parseString($type_string);
foreach ($type->getAtomicTypes() as $atomic_type) {
Expand All @@ -1839,7 +1837,7 @@ public function getCompletionItemsForArrayKeys(
return $completion_items;
}

private static function getPositionFromOffset(int $offset, string $file_contents) : Position
private static function getPositionFromOffset(int $offset, string $file_contents): Position
{
$file_contents = substr($file_contents, 0, $offset);

Expand Down Expand Up @@ -1957,7 +1955,7 @@ public function addTaintSource(
string $taint_id,
array $taints = TaintKindGroup::ALL_INPUT,
?CodeLocation $code_location = null
) : void {
): void {
if (!$this->taint_flow_graph) {
return;
}
Expand Down Expand Up @@ -1986,7 +1984,7 @@ public function addTaintSink(
string $taint_id,
array $taints = TaintKindGroup::ALL_INPUT,
?CodeLocation $code_location = null
) : void {
): void {
if (!$this->taint_flow_graph) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Config.php
Expand Up @@ -2100,7 +2100,7 @@ function () use ($vendor_autoload_files_path) {
$codebase->classlikes->forgetMissingClassLikes();

$this->include_collector->runAndCollect(
function () {
function (): void {
// do this in a separate method so scope does not leak
/** @psalm-suppress UnresolvableInclude */
require $this->autoloader;
Expand Down
10 changes: 5 additions & 5 deletions src/Psalm/Config/Creator.php
Expand Up @@ -56,7 +56,7 @@ public static function getContents(
?string $suggested_dir,
int $level,
string $vendor_dir
) : string {
): string {
$paths = self::getPaths($current_dir, $suggested_dir);

$template = str_replace(
Expand Down Expand Up @@ -90,7 +90,7 @@ public static function createBareConfig(
string $current_dir,
?string $suggested_dir,
string $vendor_dir
) : Config {
): Config {
$config_contents = self::getContents($current_dir, $suggested_dir, 1, $vendor_dir);

return Config::loadFromXML($current_dir, $config_contents);
Expand All @@ -99,7 +99,7 @@ public static function createBareConfig(
/**
* @param array<IssueData> $issues
*/
public static function getLevel(array $issues, int $counted_types) : int
public static function getLevel(array $issues, int $counted_types): int
{
if ($counted_types === 0) {
$counted_types = 1;
Expand Down Expand Up @@ -210,7 +210,7 @@ public static function getPaths(string $current_dir, ?string $suggested_dir): ar
* @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
private static function getPsr4Or0Paths(string $current_dir, array $composer_json) : array
private static function getPsr4Or0Paths(string $current_dir, array $composer_json): array
{
$psr_paths = array_merge(
$composer_json['autoload']['psr-4'] ?? [],
Expand Down Expand Up @@ -256,7 +256,7 @@ private static function getPsr4Or0Paths(string $current_dir, array $composer_jso
/**
* @return list<string>
*/
private static function guessPhpFileDirs(string $current_dir) : array
private static function guessPhpFileDirs(string $current_dir): array
{
$nodes = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Config/FileFilter.php
Expand Up @@ -412,10 +412,10 @@ public static function loadFromXMLElement(
return self::loadFromArray($config, $base_dir, $inclusive);
}

private static function isRegularExpression(string $string) : bool
private static function isRegularExpression(string $string): bool
{
set_error_handler(
function () : bool {
function (): bool {
return false;
},
E_WARNING
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Context.php
Expand Up @@ -760,7 +760,7 @@ public function hasVariable(string $var_name): bool
return isset($this->vars_in_scope[$var_name]);
}

public function getScopeSummary() : string
public function getScopeSummary(): string
{
$summary = [];
foreach ($this->vars_possibly_in_scope as $k => $_) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/DocComment.php
Expand Up @@ -181,7 +181,7 @@ public static function parse(string $docblock, ?int $line_number = null, bool $p
/**
* Parse a docblock comment into its parts.
*/
public static function parsePreservingLength(Doc $docblock) : ParsedDocblock
public static function parsePreservingLength(Doc $docblock): ParsedDocblock
{
$parsed_docblock = DocblockParser::parse(
$docblock->getText(),
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/ErrorBaseline.php
Expand Up @@ -258,7 +258,7 @@ private static function writeToFile(
('php:' . PHP_VERSION),
],
array_map(
function (string $extension) : string {
function (string $extension): string {
return $extension . ':' . phpversion($extension);
},
$extensions
Expand Down Expand Up @@ -302,7 +302,7 @@ function (string $extension) : string {
/**
* @param string[] $matches
*/
function (array $matches) : string {
function (array $matches): string {
return
'<files' .
"\n " .
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/FileManipulation.php
Expand Up @@ -38,14 +38,14 @@ public function __construct(
$this->remove_trailing_newline = $remove_trailing_newline;
}

public function getKey() : string
public function getKey(): string
{
return $this->start === $this->end
? ($this->start . ':' . sha1($this->insertion_text))
: ($this->start . ':' . $this->end);
}

public function transform(string $existing_contents) : string
public function transform(string $existing_contents): string
{
if ($this->preserve_indentation) {
$newline_pos = strrpos($existing_contents, "\n", $this->start - strlen($existing_contents));
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/AttributeAnalyzer.php
Expand Up @@ -32,7 +32,7 @@ public static function analyze(
array $suppressed_issues,
int $target,
?ClassLikeStorage $classlike_storage = null
) : void {
): void {
if (ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$source,
$attribute->fq_class_name,
Expand Down Expand Up @@ -186,7 +186,7 @@ private static function checkAttributeTargets(
SourceAnalyzer $source,
AttributeStorage $attribute,
int $target
) : void {
): void {
$codebase = $source->getCodebase();

$attribute_class_storage = $codebase->classlike_storage_provider->get($attribute->fq_class_name);
Expand Down
14 changes: 7 additions & 7 deletions src/Psalm/Internal/Analyzer/ClassAnalyzer.php
Expand Up @@ -666,7 +666,7 @@ public static function addContextProperties(
string $fq_class_name,
?string $parent_fq_class_name,
array $stmts = []
) : void {
): void {
$codebase = $statements_source->getCodebase();

foreach ($storage->appearing_property_ids as $property_name => $appearing_property_id) {
Expand Down Expand Up @@ -1113,7 +1113,7 @@ private function checkPropertyInitialization(
$constructor_storage = $constructor_class_storage->methods['__construct'];

$fake_constructor_params = array_map(
function (FunctionLikeParameter $param) : PhpParser\Node\Param {
function (FunctionLikeParameter $param): PhpParser\Node\Param {
$fake_param = (new PhpParser\Builder\Param($param->name));
if ($param->signature_type) {
$fake_param->setType((string)$param->signature_type);
Expand All @@ -1137,7 +1137,7 @@ function (FunctionLikeParameter $param) : PhpParser\Node\Param {
);

$fake_constructor_stmt_args = array_map(
function (FunctionLikeParameter $param) : PhpParser\Node\Arg {
function (FunctionLikeParameter $param): PhpParser\Node\Arg {
$attributes = $param->location
? [
'startFilePos' => $param->location->raw_file_start,
Expand Down Expand Up @@ -1591,7 +1591,7 @@ private static function addOrUpdatePropertyType(
Type\Union $inferred_type,
StatementsSource $source,
bool $docblock_only = false
) : void {
): void {
$manipulator = PropertyDocblockManipulator::getForProperty(
$project_analyzer,
$source->getFilePath(),
Expand Down Expand Up @@ -1849,7 +1849,7 @@ public static function analyzeClassMethodReturnType(
MethodIdentifier $analyzed_method_id,
MethodIdentifier $actual_method_id,
bool $did_explicitly_return
) : void {
): void {
$secondary_return_type_location = null;

$actual_method_storage = $codebase->methods->getStorage($actual_method_id);
Expand Down Expand Up @@ -2149,7 +2149,7 @@ private function checkImplementedInterfaces(
Codebase $codebase,
string $fq_class_name,
ClassLikeStorage $storage
) : bool {
): bool {
$classlike_storage_provider = $codebase->classlike_storage_provider;

foreach ($class->implements as $interface_name) {
Expand Down Expand Up @@ -2425,7 +2425,7 @@ private function checkParentClass(
ClassLikeStorage $storage,
Codebase $codebase,
?Context $class_context
) : void {
): void {
$classlike_storage_provider = $codebase->classlike_storage_provider;

if (!$parent_fq_class_name) {
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/ClassLikeAnalyzer.php
Expand Up @@ -184,7 +184,7 @@ public function getMethodMutations(
}
}

public function getFunctionLikeAnalyzer(string $method_name) : ?MethodAnalyzer
public function getFunctionLikeAnalyzer(string $method_name): ?MethodAnalyzer
{
foreach ($this->class->stmts as $stmt) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod &&
Expand Down Expand Up @@ -639,7 +639,7 @@ public static function getClassesForFile(Codebase $codebase, string $file_path):
}
}

public function getFileAnalyzer() : FileAnalyzer
public function getFileAnalyzer(): FileAnalyzer
{
return $this->file_analyzer;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/ClosureAnalyzer.php
Expand Up @@ -68,7 +68,7 @@ public static function analyzeExpression(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\FunctionLike $stmt,
Context $context
) : bool {
): bool {
$closure_analyzer = new ClosureAnalyzer($stmt, $statements_analyzer);

if ($stmt instanceof PhpParser\Node\Expr\Closure
Expand Down Expand Up @@ -230,7 +230,7 @@ public static function analyzeClosureUses(
Context $context
): ?bool {
$param_names = array_map(
function (PhpParser\Node\Param $p) : string {
function (PhpParser\Node\Param $p): string {
if (!$p->var instanceof PhpParser\Node\Expr\Variable
|| !is_string($p->var->name)
) {
Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Analyzer/CommentAnalyzer.php
Expand Up @@ -77,7 +77,7 @@ public static function arrayToDocblocks(
Aliases $aliases,
?array $template_type_map = null,
?array $type_aliases = null
) : array {
): array {
$var_id = null;

$var_type_tokens = null;
Expand Down Expand Up @@ -211,7 +211,7 @@ public static function arrayToDocblocks(
private static function decorateVarDocblockComment(
VarDocblockComment $var_comment,
ParsedDocblock $parsed_docblock
) : void {
): void {
$var_comment->deprecated = isset($parsed_docblock->tags['deprecated']);
$var_comment->internal = isset($parsed_docblock->tags['internal']);
$var_comment->readonly = isset($parsed_docblock->tags['readonly'])
Expand Down Expand Up @@ -252,7 +252,7 @@ private static function decorateVarDocblockComment(
/**
* @psalm-pure
*/
public static function sanitizeDocblockType(string $docblock_type) : string
public static function sanitizeDocblockType(string $docblock_type): string
{
$docblock_type = preg_replace('@^[ \t]*\*@m', '', $docblock_type);
$docblock_type = preg_replace('/,\n\s+\}/', '}', $docblock_type);
Expand Down

0 comments on commit 39402c2

Please sign in to comment.