Skip to content

fix: a string has contains, lowercase, uppercase, startsWith, endsWith, replace and split, and a List has flatMap and toList - #167

Merged
heyJordanParker merged 8 commits into
masterfrom
fix/string-members
Oct 18, 2026
Merged

heyJordanParker merged 8 commits into
masterfrom
fix/string-members

Conversation

@heyJordanParker

Copy link
Copy Markdown
Owner

Problem

Validating PHP# on a real Laravel app showed that everyday text and list operations were missing. text.contains("a"), text.lowercase(), text.uppercase(), text.startsWith("a"), text.endsWith("a"), text.replace("a", "b") and line.split(",") were refused as a method on a non-object string. rows.flatMap(r => r) was refused as a method List does not have, and so was counts.values().toList(). The moved code fell back on global.str_contains and the other PHP functions, which spec section 8 says PHP# code never needs.

Solution

  • Sharp.Text.Text's extensions on string gain the seven text members with Kotlin's names. Case changes run in UTF-8, so "ÉCOLE".lowercase() is "école". replace replaces every match. split keeps the empty text after a trailing delimiter, and an empty delimiter throws ValueError("Text.split: the delimiter is empty").
  • Each PHP function a member replaces is refused bare and through global, naming the member, as section 8 requires: str_contains, strtolower, mb_strtolower, strtoupper, mb_strtoupper, str_starts_with, str_ends_with, str_replace and explode. explode's refusal writes line.split(…) on its second argument, and str_replace's writes name.replace(…) on its third.
  • List<T>.flatMap runs on the engine's Sharp\Collection. It reads each Iterable its function gives as a Sequence reads its source: a List, a Set or a generator. A function that gives no Iterable throws TypeError.
  • List<T>.toList() lowers to the list itself, so toList() works on every Iterable: a List, a Set, a Map's values and a lazy chain.
  • The engine fixtures that called the replaced functions through global now call the members. Section 14.3's function value example reads global.ucfirst, which the library does not replace.
php-sharp/
├── docs/spec.md                                        section 18: text-methods; section 12: flat-map-to-list; section 14.3 reads global.ucfirst
├── ext/sharp/sharp.c, sharp.stub.php, sharp_arginfo.h  Sharp\Collection::flatMap
├── sharp/composer/library/Sharp/Text/Text.sharp        the seven text members
├── sharp/mago/crates/
│   ├── analyzer/src/expression/call/function_call.rs  nine REPLACEMENTS rows; Replacement::MembersOn
│   ├── names/src/lib.rs                                (List, toList) inlined
│   ├── prelude/assets/extensions/sharp.php             ListMethods::flatMap and toList
│   └── sharp-bridge/src/lower.rs                       toList lowers to the list
└── Zend/tests/sharp/
    ├── library_text{.phpt,.inc,_preload.phpt}         a row per text member, with and without preloading
    ├── ListMembers/, list_members{,_preload}.phpt     flatMap and toList, with and without preloading
    └── 12 fixtures                                     the members in place of global.strtoupper, strtolower and explode

No file in sharp/harness/ changed.

…h, replace and split, and a List has flatMap and toList

Problem

A PHP# developer moving a Laravel app's code reached for everyday text and
list operations and found none. `text.contains("a")`, `text.lowercase()`,
`text.startsWith("a")`, `text.endsWith("a")`, `text.replace("a", "b")` and
`line.split(",")` were refused as a method on a non-object `string`,
`rows.flatMap(r => r)` as a method `List` does not have, and
`counts.values().toList()` too, since a `List` had no `toList`. The moved
code fell back on `global.str_contains` and the other PHP functions, which
spec section 8 says PHP# code never needs.

Solution

The standard library's `Sharp.Text.Text` extensions on `string` gain the
seven text members with Kotlin's names. Case changes run
`mb_strtolower` and `mb_strtoupper` in UTF-8, so `"ÉCOLE".lowercase()` is
`"école"`. `replace` replaces every match, `split` keeps the empty text
after a trailing delimiter, and an empty delimiter throws
`ValueError("Text.split: the delimiter is empty")`.

Each PHP function a member replaces is now refused through `global` and
bare, naming the member, as section 8 requires: `str_contains`,
`strtolower`, `mb_strtolower`, `strtoupper`, `mb_strtoupper`,
`str_starts_with`, `str_ends_with`, `str_replace` and `explode`. A
replacement row can name the argument the member runs on, so `explode`'s
refusal writes `line.split(…)` on its second argument and `str_replace`'s
writes `name.replace(…)` on its third.

`List<T>.flatMap` runs on the engine's `Sharp\Collection`, reading each
Iterable its function gives as a Sequence reads its source: a List, a Set
or a generator. A function that gives no Iterable throws TypeError.
`List<T>.toList()` lowers to the list itself, which PHP copies on a write,
so `toList()` now works on every Iterable: a List, a Set, a Map's values
and a lazy chain.

The engine fixtures that called the replaced functions through `global`
call the members, and their PHP twins call `mb_strtoupper` and
`mb_strtolower` as the inlined members do. Section 14.3's function value
example reads `global.ucfirst`, which the library does not replace.

php-sharp/
├── docs/spec.md                                         section 18: text-methods; section 12: flat-map-to-list; section 14.3 reads global.ucfirst
├── ext/sharp/
│   ├── sharp.c                                          Sharp\Collection::flatMap; sharp_sequence_append, shared with Sequence::toList
│   ├── sharp.stub.php                                   flatMap
│   └── sharp_arginfo.h                                  regenerated
├── sharp/composer/library/Sharp/Text/Text.sharp         the seven text members
├── sharp/mago/crates/
│   ├── analyzer/src/expression/call/function_call.rs   nine REPLACEMENTS rows; Replacement::MembersOn names the receiver's argument
│   ├── analyzer/tests/sharp.rs                          flatMap and toList types; the receiver's argument in a refusal
│   ├── names/src/lib.rs                                 (List, toList) inlined
│   ├── prelude/assets/extensions/sharp.php              ListMethods::flatMap and toList
│   └── sharp-bridge/{src,tests}/lower.rs                toList lowers to the list
└── Zend/tests/sharp/
    ├── TextCalls.sharp, library_text.phpt               a row per text member
    ├── library_text.inc                                 new: the files both runs load
    ├── library_text_preload.phpt                        new: the text members under preloading
    ├── ListMembers/Shelf.sharp                          new: flatMap and toList
    ├── list_members.phpt                                new: the rows
    ├── list_members_preload.{inc,phpt}                  new: the rows under preloading
    ├── yield.phpt                                       loads Sharp.Text.Text for split
    └── Accessors/, Behaviors/, Constructors/, FunctionValues/, Import/, Overrides/, ParameterProperties/,
        Preloaded/, Page, Suit                           the members in place of global.strtoupper, strtolower and explode
- flatMap over Maps gives their [key, value] entries: a collection
  method's function gives PHP#'s Iterable<R>, and a type argument a
  tuple fixes stays that tuple.
- replace with empty text to replace throws ValueError, and both of
  its messages and split's name the member as the code writes it.
- split takes Kotlin's limit, and Http splits a header line with it.
- A Map has toList(), which gives its entries as List<[K, V]>.
- An issue on an extension call names the member on the type it
  extends, as string.contains, and counts arguments from the first
  one inside (…).
- An unknown method on a string, an int, a float or a bool lists the
  methods it has, and never says "non-object type".
- flatMap's argument error shows its open type parameter as its bound.
- The replaced PHP functions are gone from the test stubs, the slice
  fixture and the doc comments.
Section 19 gives a string compareTo, so the text-methods example no
longer states it as a compile error, and the unknown-method rows use
shuffled.
An extension on a Set, a Map, an Iterable and a union names its member
on that type, a float lists its methods, and a receiver with no type,
which no call reaches, is unreachable.
# Conflicts:
#	sharp/mago/crates/analyzer/src/invocation/arguments.rs
@heyJordanParker
heyJordanParker merged commit 2768866 into master Oct 18, 2026
24 checks passed
@heyJordanParker
heyJordanParker deleted the fix/string-members branch November 3, 2026 22:08
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