Skip to content

Type safety: strict_types, final class, return types, private state (roadmap #3)#14

Merged
mmucklo merged 1 commit into
masterfrom
type-safety
Apr 13, 2026
Merged

Type safety: strict_types, final class, return types, private state (roadmap #3)#14
mmucklo merged 1 commit into
masterfrom
type-safety

Conversation

@mmucklo
Copy link
Copy Markdown
Owner

@mmucklo mmucklo commented Apr 12, 2026

Addresses ROADMAP.md item #3. Stacked on #13 (which is stacked on #12).

Changes

  • declare(strict_types=1) at the top of the class.
  • final class Inflect — the class was never designed for inheritance; locale support (roadmap §5a) will plug in via composition.
  • Param and return types:
    • pluralize(string $string): string
    • singularize(string $string): string
    • pluralizeIf(int $count, string $string): string
    • preserveFirstCase(string $source, string $replaced): string
  • Private static state — the rule tables ($plural, $singular, $irregular, $uncountable) and memoization caches are now private. The extensibility API (roadmap §5) will own the supported ways to add rules.
  • Typed properties on all static arrays (PHP 7.4+ syntax).
  • Short array syntax throughout ([] instead of array()).
  • ===== in pluralizeIf count check.

Nullable input decision

Current behavior: pluralize(null) silently returned null via the falsy-truthy check at the top; pluralize('') did the same.

New behavior:

  • pluralize(null) / singularize(null)TypeError. Callers with null are now forced to handle it explicitly.
  • pluralize('') / singularize('') → returns ''. No behavior surprise for empty-string inputs.

This is a breaking change for callers relying on the silent-null behavior — acceptable for the v2.0 major bump.

Test plan

  • Added explicit empty-string coverage for pluralize and singularize.
  • Added a data-provider set for pluralizeIf including the zero case and irregulars (person → people, datum → data).
  • 117 tests / 118 assertions pass on PHP 8.1 and 8.3.
  • CI from Replace Travis with GitHub Actions + Codecov + Scrutinizer (roadmap #2) #13 should run green on 8.1–8.4 once this PR rebases onto a merged base.

Stacking

This closes the v2.0 trio: #12 (PHP baseline) → #13 (GitHub Actions CI) → #14 (this). Suggest merging in that order.

🤖 Generated with Claude Code

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 12, 2026

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Base automatically changed from add-github-actions-ci to master April 13, 2026 01:48
Roadmap item #3.

- Add declare(strict_types=1).
- Mark Inflect as final — the class was never designed to be extended,
  and locale support (roadmap §5a) will plug in via composition, not
  inheritance.
- Add string/int param types and string return types to pluralize,
  singularize, pluralizeIf, and preserveFirstCase.
- Make static rule tables (plural, singular, irregular, uncountable)
  and caches private — the extensibility API (roadmap §5) will own the
  supported ways to add rules.
- Type the static property arrays with PHP 7.4+ typed-property syntax.
- Convert array() to [] throughout the class.
- Nullable input decision: accept string (not nullable), return empty
  string for empty input. Callers passing null now get a TypeError
  rather than a silent null return — acceptable for the v2.0 major bump.
- Switch == to === in pluralizeIf count check.

Tests:
- Add explicit coverage for empty-string behavior on both methods.
- Add a data-provider case set for pluralizeIf, including the zero
  case and an irregular (person, datum).

117 tests / 118 assertions pass on PHP 8.1 and 8.3.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mmucklo mmucklo merged commit 96f69d0 into master Apr 13, 2026
6 of 7 checks passed
@mmucklo mmucklo deleted the type-safety branch April 13, 2026 01:55
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.

1 participant