Array cast should emit an error

Open
#190 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
tooling

Research direction

Start by locating where PHPStan strict rules inspect PHP casts and where their tests are defined; the issue names no files or tests. Use the linked playground and 3v4l example to confirm the expected behavior, then add coverage showing that an (array) cast emits an error, including the case on playground line 10.

Written by the indexing model from the issue text.

Description

Feature Request

Array cast is redundant on array type. (redundant cast emit a phpstan error already)

For scalar type values it wraps the value into an array - inconsistent with casting an array, array is not wrapped again.

For null it produces empty array - can be a mistake in code, scalar type value is wrapped in an array and never empty.

For objects - get_object_vars should be used.

Given these reasons, I would expect phpstan to emit an error if an (array) cast is found.

demo: https://3v4l.org/2INk8

playground: https://phpstan.org/r/e03daaef-49f6-4c30-b029-24bed8f50048 (line 10 should emit an error)

Dominant language
PHP
Stars
709
Forks
62
Avg merge
5d 19h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-strict-rules

All issues in phpstan/phpstan-strict-rules

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.