Skip to content

PHPLIB-675 Add helpers to check collection types to CollectionInfo #839

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

Merged
merged 3 commits into from
Jul 8, 2021

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Jul 5, 2021

PHPLIB-675

This adds getType, getInfo, and getIdIndex. Methods relating to capped collections have been deprecated in documentation.

@alcaeus alcaeus requested a review from jmikola July 5, 2021 19:49
@alcaeus alcaeus self-assigned this Jul 5, 2021
public function isCollection() : bool
{
return $this->info['type'] == 'collection';
}
Copy link
Member

Choose a reason for hiding this comment

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

For a moment, let's ignore the capped method we already have. Based on https://docs.mongodb.com/manual/reference/command/listCollections/#mongodb-data-listCollections.cursor, would it make more sense to just create getters for the missing top-level fields?

These three methods could be replaced with getType() : string. Additionally, we could introduce getInfo() : array for the info field, which would presumably cast like getOptions does. I would be a bit concerned that "info" there might confuse users into thinking it returns the entire IndexInfo object as an array -- so I'm open to ways we can avoid the misunderstanding.

I have no idea what idIndex actually looks like, but I wonder if that'd be problematic to cast to an array as well.

While this class does implement ArrayAccess, I don't think it's easy to know exactly what can be accessed. It'd be lousy to call __debugInfo() directly, but that basically returns the value someone might want to have full access. I'm open to other ideas to make this most accessible.

Copy link
Member Author

@alcaeus alcaeus Jul 6, 2021

Choose a reason for hiding this comment

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

I was considering this as well. I'm thinking this:

interface CollectionInfo
{
    /** Returns the name of the collection or view */
    public function getName() : string;

    /** Returns info.type */
    public function getType() : string;

    /** Returns info.options */
    public function getOptions() : array;

    /** Returns the raw collection info response from listCollections */
    public function getRawInfo() : array;
}

I'm not sure what to do with the isCapped helper though, whether to keep or deprecate it.

A different, more involved approach, would see different classes for the various types exposed through CollectionInfo:

/** @final */
class Collection implements CollectionInfo
{
}

final class CappedCollection extends Collection
{
    public function getMax() : ?int {}

    public function getSize() : ?int {}
}

final class View implements CollectionInfo
{
}

Copy link
Member

Choose a reason for hiding this comment

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

Giving this some more though: If I knew nothing about this class, I think my first impression of getRawInfo would be the method that returns everything (like __debugInfo). I think getInfo makes more sense and we can just clearly document that it returns the "info" property of the server response.

I'm not sure what to do with the isCapped helper though, whether to keep or deprecate it.

I think we can deprecate it in documentation. That would give us something to point to if an idea for other specific helpers comes up down the line.

A different, more involved approach, would see different classes for the various types exposed through CollectionInfo:

While that would be very usable (folks could use instanceof), I don't think we have the data to invest in this API -- and that's basically taking us down a road of query builders where we'd need to maintain this for each server update. Perhaps worse because I don't think the server response is fully documented.

This does seem like something more fitting for an auxiliary library (PHPLIB-4), which is where we originally intended to build out APIs for other shell-like commands (e.g. user management, misc. helpers).

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I've added getters for idIndex, info, and type which previously didn't have any. I also added @deprecated phpdoc comments to the isCapped, getCappedMax, and getCappedSize methods, but they don't trigger any deprecations warnings yet.

@alcaeus alcaeus force-pushed the phplib-675 branch 2 times, most recently from 68ee895 to 3b98d88 Compare July 7, 2021 08:43
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

LGTM with two suggestions.

@@ -59,6 +59,8 @@ public function __debugInfo()
/**
* Return the maximum number of documents to keep in the capped collection.
*
* @deprecated Deprecated in favor of using getOptions
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with the other two places we use @deprecated (ChangeStream and Collection), I think this should include the version in which we deprecate it. Consider:

@deprecated 1.9 Deprecated in favor of using getOptions

$this->assertSame([], $info->getOptions());
$this->assertFalse(isset($info['options']));
Copy link
Member

Choose a reason for hiding this comment

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

FYI: you can use assertArrayNotHasKey here for more verbose messages. ArrayHasKey constraint supports ArrayAccess and invokes offsetExists.

@alcaeus alcaeus merged commit 1685ebc into mongodb:master Jul 8, 2021
@alcaeus alcaeus deleted the phplib-675 branch July 8, 2021 07:33
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.

2 participants