Problem/Motivation
When you call \Drupal::service('date.formatter')->format($timestamp, $type = '', $format = 'd.d.d.d') Drupal tries to load the date format based on the type because it is not custom but will not found any format for empty string. This can lead to unnecessary calls.
See profiler images before/after in #3519986: Set missing date format type.
Steps to reproduce
Proposed resolution
Teach people via phpdoc non-empty-string (https://phpstan.org/writing-php-code/phpdoc-types#other-advanced-string-...) and verify the type is not falsely before try to load the format.
Remaining tasks
Ensure test-only test is failing.
User interface changes
N/A
Introduced terminology
N/A
API changes
N/A-
Data model changes
N/A-
Release notes snippet
N/A-
Issue fork drupal-3520188
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:
- 3520188-empty-date-format-type
changes, plain diff MR !11893
Comments
Comment #3
tobiasbComment #4
tobiasbComment #5
smustgrave commentedRan the test-only feature and unfortunately it passed when it was expected to fail. So think the test needs a little tweaking
Comment #6
tobiasbWith NULL as value, we get an error.
Cannot load the "date_format" entity with NULL ID.Comment #7
oily commentedHere is the test-only output:
Comment #8
oily commentedRe #5, as stated in #6 the test-only test is failing as described in #6.
Updated the IS.
Moving to RTBTC, but not sure. With the test failing does that mean that it does not run the assertions later in the file? It stops at the failure? If so, is there another assertion that could be used that would not stop the test?
The error message comes from line 261 in EntityStorageBase.php:
Could an assertion like this one be used instead from line 566 in ConfigEntityStorageTest.php:
Comment #9
oily commentedComment #10
oily commentedComment #11
oily commentedAfter trying things mentioned in #8 have abandoned the attempt.
Comment #12
tobiasbComment #13
tobiasbI replaced the kernel test with a unit test.
A test only would result into:
Comment #14
mstrelan commentedCan we possibly use the existing unit test at
\Drupal\Tests\Core\Datetime\DateTest?Comment #15
tobiasb@mstrelan
Done. Somehow I could not believe that we do not have a unit test for the date formatter. :D
Comment #16
mstrelan commentedOnly other suggestion is that we could also update the @param doc in
DateFormatter::dateFormattonon-empty-stringand potentially also expand the doc for$dateFormatsproperty to something likearray<non-empty-string, array<string, \Drupal\Core\Datetime\DateFormatInterface>>, but not sure we want to include that here, so setting to RTBC.Comment #20
catchCommitted/pushed to 11.x and cherry-picked to 11.3.x, thanks!