Disallow offset access on string

Open
#218 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reviewing the existing strict rules for string operations and their tests, then trace how rules are registered and how PHPStan reports violations. The issue's examples define the expected rejected access forms and the preferred replacement; done means those cases are detected without affecting unrelated string usage.

Written by the indexing model from the issue text.

Description

I propose to add, to strict rules, a rule to reject string offset access like:

$str[$index];
$str[5];

in favor of

substring($str, $index, 1)
substring($str, 5, 1)

The main motivation is to detect wrongly declared types. Currently when $var is declared wrongly as string instead of string[], it is undetected.

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.