Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator from class #46

Closed
jtojnar opened this issue Apr 10, 2022 · 1 comment
Closed

Validator from class #46

jtojnar opened this issue Apr 10, 2022 · 1 comment

Comments

@jtojnar
Copy link

jtojnar commented Apr 10, 2022

The Expect::from is nice, but it does not really work for immutable objects like the following:

<?php

use Money\Money;

final class InvoiceItem {
	public function __construct(
		public readonly string $name,
		public readonly Money $price,
		public readonly int $amount,
	) {
	}
}
Version without readonly
<?php

use Money\Money;

final class InvoiceItem {
	public function __construct(
		private string $name,
		private Money $price,
		private int $amount,
	) {
	}

	public function getName(): string {
		return $this->name;
	}

	public function getPrice(): Money {
		return $this->price;
	}

	public function getAmount(): int {
		return $this->amount;
	}
}

It would be nice to have Expect::fromClass(InvoiceItem::class), that would check the constructor arguments instead of the public properties like Expect::from does.

I can try to implement this, if you think this is a good idea.

@jtojnar
Copy link
Author

jtojnar commented Apr 10, 2022

Actually, I am not sure if argument names are considered a part of the public API. But with PHP 8’s support for named parameters, they probably should be anyway.

Edit: Looks like it is indeed the case and some projects are using @no-named-arguments as an annotation to disable that. So I would suggest supporting all classes whose constructor is not annotated with that.

@dg dg closed this as completed in ac25085 Oct 3, 2023
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

No branches or pull requests

1 participant