Skip to content

Conversation

@maciejmrozinski
Copy link

Things done:

  • Added more IDE friendly comments
  • Return FulfilledPromise for ArrayCache implementation
  • Modify tests with Promise checks

/**
* @param $key
* @return PromiseInterface
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all doc blocks can be removed from ArrayCache as they are inherited from CacheInterface.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right.

$that = $this;
$setPromise->then(function ($true) use ($that) {
$that->assertTrue($true);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should probably be like

$mock = $this->createCallableMock();
$mock
    ->expects($this->once())
    ->method('__invoke');
    ->with($this->identicalTo(true))
;
$setPromise->then($mock);

Same for removeShouldRemoveKey().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/**
* @param $key
* @param $value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allowed types for $key and $value should probably be added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The allowed types for $key and $value should probably be added.

Strictly speaking, defining types for key and value are out of the scope of this PR. IMHO, the defintions should stay like this or removed at all.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they don't exist, they're out of scope, yes. But in that case, those @params are just noise and can be deleted.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed them.

Copy link
Member

@jsor jsor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public function set($key, $value);

/**
* @return PromiseInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add here what was expect and/or allow to be resolved through the promise. Is it just a simple bool or do we allow implementer specific information to be returned?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question and i though about that myself lately. Since the returned promise is just a placeholder for a value, i'd be great if the types of the actual value the placeholder will resolve to could be defined.

Maybe ask the authors of the PSR PHPDoc Standard?

For now, this should probably just stated in the description, eg.

@return PromiseInterface A promise which resolves to true on success.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if true (or bool more generally) is a good idea. In Redis cache, the returning values are responses directly from Redis server (int, string, etc.). In example, for successful save operation "OK" string is returned as Redis returns it.

This is a general problem with Promises, that function should be described with PromiseInterface (to make IDE life easier and ours too) but we don't have any knowledge then about the value returned by this Promise itself when fulfilled. I saw in few libraries that declared return value was: PromiseInterface|int|string. We can simplify this to PromiseInterface|mixed. This will help code completition ans also give some tip about value retrieved from Promise itself. Implementing libraries can then narrow the return value to PromiseInterface|string or PromiseInterface|MyGreatClass.

Just a suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good compromise 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maciejmrozinski It doesn't help code completion, as mixed etc. isn't returned, it's the resolution value. The proper way is either using generics like Promise<int> or using @return int and an additional @promise or @async. That way, also @throws could be used. While this is a general problem in PHP, promises are that often in async programming that I think it's fine to introduce new PHPDoc tags for them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the Promise<int> notation but that is just personal 😄 . What is more important that we define here what the developer can be expected to get from the promise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be the best place to discuss this topic. This probably more suited for a meta discussion. For now, i'd propose to simply define the the possible resolution values in the description.

/**
 * @return PromiseInterface A promise which resolves to TRUE if the entry was successfully stored in the cache, FALSE otherwise.
 */
public function set($key, $value);

/**
 * @return PromiseInterface A promise which resolves to TRUE if the cache entry was successfully removed, FALSE otherwise.
 */
public function remove($key);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it just resolve to null and failure to store it in the cache be an exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"There was an error, but I won't tell you where or why it failed."

The reason for PSR-16 not to use an exception was that a failing cache shouldn't interrupt your code and keep things working. In case of promises you can just not wait for the promise or subscribe to it's resolution at all if you don't care.

@jsor jsor added this to the v0.5.0 milestone Sep 8, 2017
@jsor jsor requested review from WyriHaximus and clue September 8, 2017 19:56
Copy link
Member

@WyriHaximus WyriHaximus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maciejmrozinski Could you make the changes about what the expected behavior is as discussed? Like to move forward with this issue and react/cache v0.5.0 in general

@WyriHaximus
Copy link
Member

@maciejmrozinski I've opened a new PR based on this one, thanks for your work so far 👍

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

Successfully merging this pull request may close these issues.

4 participants