Closed remcotolsma closed 10 months ago
As $id
is already casted to int
, we can also accept string
as $post_id
in PaymentsDataStoreCPT::get_payment()
and by re-ordering some code we can prevent calling array_key_exists()
for null
and false
values, like so:
/**
* Get payment by ID.
*
- * @param int $id Payment ID.
+ * @param int|string $id Payment ID.
* @return Payment|null
*/
public function get_payment( $id ) {
+ $id = (int) $id;
+
+ if ( empty( $id ) ) {
+ return null;
+ }
+
if ( \array_key_exists( $id, $this->payments ) ) {
return $this->payments[ $id ];
}
- if ( empty( $id ) ) {
- return null;
- }
-
- $id = (int) $id;
$post_type = \get_post_type( $id );
if ( 'pronamic_payment' !== $post_type ) {
return null;
}
The method PaymentsDataStoreCPT::get_payment()
only seems to be called through get_pronamic_payment()
. The $post_id
is passed to PaymentsDataStoreCPT::get_payment()
straight away, but the documented parameters are incompatible:
I don't think it makes sense to get a payment from the data store for bool
and null
values in get_pronamic_payment()
and suggest returning null
for non int
and string
values instead of trying to fetch them from the data store in these cases (e.g. bool
and null
, but that would also prevent calling PaymentsDataStoreCPT::get_payment()
for array
, float
values etc.). I think we can expect any type, as get_pronamic_payment()
is kind of a template function and can be used by users like so.
function get_pronamic_payment( $post_id ) {
if ( ! is_int( $post_id ) && ! is_string( $post_id ) ) {
return null;
}
return pronamic_pay_plugin()->payments_data_store->get_payment( $post_id );
}
https://github.com/pronamic/wp-pay-core/blob/f260bc3b6cf38737260e075d7098ce679e3a3f20/src/Payments/PaymentsDataStoreCPT.php#L69-L78
CC @rvdsteege