Ran into an issue...user wants to put a timestamp in their body text like [12:57] - this is being seen as a token by smart_trim and all other modules that look for tokens, but long story short my error:
"smart_trim_tokens(): Argument #1 ($type) must be of type string, int given"
I went to that function which is using HOOK_tokens and see the $type is expecting a string, but if I look at the core HOOK_tokens, the $type argument is not hinted. If we remove this hint, my error goes away. All other contrib modules I looked at are not type-hinting this argument either, so I say we follow the core example which is:
function hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {}
Looks like smart_trim is hinting the $tokens argument as well, so we could remove that as well.
| Comment | File | Size | Author |
|---|
Issue fork smart_trim-3479519
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
porchlight commentedPatch attached
Comment #3
dpovshed commentedThe fix looks good!
Comment #4
mstrelan commentedRelated core issue. Probably string is correct but the regex is finding ints.
Comment #6
mstrelan commentedCreated MR that applies to 2.x-dev branch and 2.3.1 release.
Comment #7
lostcarpark commentedAlthough it pains me to remove strong typing, it does seem to be necessary in this case.
I note composer is failing for "next major". I think that is probably due to Drupal 12 development, and should probably be resolved in a separate issue.
Comment #8
mstrelan commentedTrust me, I agree. You could maybe opt to use a union type of int|string|float. Also, I think D12 is broken due to drush incompatibility with Symfony 8.
Comment #9
csakiistvanEnvironment
3479519-incorrect-type-hinting)Prerequisites
smart_trimand its dependencytokenenabled.strict_types=1, so the parameter type mismatch is enforced at call time.Root cause
Core's token scanner returns the token type as an
intwhen it is numeric (a token such as[12:57]yields type12, because PHP casts numeric array keys to integers). Thehook_tokens()implementation type-hintedstring $type(andarray $tokens), which does not match core's ownhook_tokens()signature (unhinted). Under a strict caller this throwsTypeError: Argument #1 ($type) must be of type string, int given.Steps
stringhint from$typeand thearrayhint from$tokensin thehook_tokens()implementation, matching core's unhinted signature.ddev drush cr[12:57], so the token type reaches the hook as an integer.TypeError.Expected results
int) is accepted by thehook_tokens()implementation.TypeError.[entity:field-smart-trim]token replacement continues to work.Actual results
Before the fix, passing the scanned numeric token type (
int 12) into the type-hintedstring $typeparameter understrict_types=1raisedTypeError: Drupal\smart_trim\Hook\SmartTrimTokenHooks::tokens(): Argument #1 ($type) must be of type string, int given. After removing the$typeand$tokenshints, the same numeric token type is accepted and token generation completes without error, while existing smart-trim token replacement is unaffected.Testing produced with the assistance of an LLM.
Comment #10
csakiistvanComment #11
markie commentedStill needs community review
Comment #12
csakiistvanComment #14
ultimikeI went ahead and updated the issue fork with latest from 2.x (we do not have a `smart_trim.tokens.inc` anymore.)
I usually prefer to type hint all method/function arguments, but since we're following an established pattern from Drupal core, I guess I'll have to get over it.
I am moving this back to "Needs work" for two reasons:
Drupal\Tests\smart_trim\Kernel\TokenTest::testSummaryFieldTokens("Token value for [node:body-smart-trim] was not generated.")-mike
Comment #16
peri22 commentedI added two fixes on top of #13:
I also added a new test: TokenTest::testNumericTokenType(). It calls the hook implementations directly. This is necessary because Drupal core dispatches hooks from non-strict code (where integers are coerced into strings). By calling it directly, we can correctly reproduce and test the reported error ("TypeError: smart_trim_tokens(): Argument #1 ($type) must be of type string, int given").
Pipeline is green!
Comment #18
ultimikeThanks everybody - merged!
-mike