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

Added else argument to ifPresent #31

Merged
merged 1 commit into from
Jan 22, 2025
Merged

Added else argument to ifPresent #31

merged 1 commit into from
Jan 22, 2025

Conversation

petrknap
Copy link
Owner

@petrknap petrknap commented Jan 22, 2025

  • This pull request does not contain any breaking change.
  • I asked the AI to briefly summarize, use and improve the feature(s), and I'm okay with the output:

Could you briefly summarize the following code for me?

OptionalResource::ofFalsable(tmpfile())->ifPresent(function ($tmpFile): void {
    fwrite($tmpFile, 'data');
    fclose($tmpFile);
}, else: fn () => print('tmpfile() failed'));

Certainly! This PHP code snippet demonstrates the use of an OptionalResource to handle a potentially false value returned by tmpfile(). Here's a breakdown:

  1. OptionalResource::ofFalsable(tmpfile()) wraps the result of tmpfile() in an OptionalResource object.
  2. The ifPresent method takes two arguments: a function to execute if the tmpfile() was successful, and an alternative function if it failed.
  3. If the tmpfile() is successful, it writes the string 'data' to the temporary file and then closes it.
  4. If the tmpfile() fails, it prints 'tmpfile() failed'.

So, in short, this code snippet ensures that appropriate actions are taken based on whether tmpfile() succeeds or fails. Let me know if you need further details!

@petrknap petrknap self-assigned this Jan 22, 2025
@petrknap petrknap added the enhancement New feature or request label Jan 22, 2025
@petrknap petrknap merged commit 8c5c811 into main Jan 22, 2025
2 checks passed
@petrknap petrknap deleted the if-present-else branch January 22, 2025 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant