From af42e76bb1e641c442ca33a0c2e14d0ba933c3f6 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Wed, 18 Mar 2015 17:44:50 -0400 Subject: [PATCH 1/3] Move pedantic method declaration test to its own file Related to: 495e46d4ddb3980126635f2397e4301d94f6c262 --- tests/CollectionTest.php | 32 -------------------- tests/PedantryTest.php | 63 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 32 deletions(-) delete mode 100644 tests/CollectionTest.php create mode 100644 tests/PedantryTest.php diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php deleted file mode 100644 index 5d9964407..000000000 --- a/tests/CollectionTest.php +++ /dev/null @@ -1,32 +0,0 @@ - ReflectionMethod::IS_PUBLIC, - 'protected' => ReflectionMethod::IS_PROTECTED, - 'private' => ReflectionMethod::IS_PRIVATE, - ); - - foreach ($filters as $visibility => $filter) { - $methods = array_map( - function(ReflectionMethod $method) { return $method->getName(); }, - $class->getMethods($filter) - ); - - $sortedMethods = $methods; - sort($sortedMethods); - - $this->assertEquals($methods, $sortedMethods, sprintf('%s methods are declared alphabetically', ucfirst($visibility))); - } - } -} diff --git a/tests/PedantryTest.php b/tests/PedantryTest.php new file mode 100644 index 000000000..46dc96715 --- /dev/null +++ b/tests/PedantryTest.php @@ -0,0 +1,63 @@ +getMethods(); + + $getSortValue = function(ReflectionMethod $method) { + if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) { + return '2' . $method->getName(); + } + if ($method->getModifiers() & ReflectionMethod::IS_PROTECTED) { + return '1' . $method->getName(); + } + if ($method->getModifiers() & ReflectionMethod::IS_PUBLIC) { + return '0' . $method->getName(); + } + }; + + $sortedMethods = $methods; + usort( + $sortedMethods, + function(ReflectionMethod $a, ReflectionMethod $b) use ($getSortValue) { + return strcasecmp($getSortValue($a), $getSortValue($b)); + } + ); + + $methods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $methods); + $sortedMethods = array_map(function(ReflectionMethod $method) { return $method->getName(); }, $sortedMethods); + + $this->assertEquals($sortedMethods, $methods); + } + + public function provideProjectClassNames() + { + $classNames = array(); + $srcDir = realpath(__DIR__ . '/../src/'); + + $files = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcDir)), '/\.php$/i'); + + foreach ($files as $file) { + $classNames[][] = 'MongoDB\\' . str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getRealPath(), strlen($srcDir) + 1, -4)); + } + + return $classNames; + } +} From 4dd165942996eb43a696e397116daf4df5f149e9 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 27 Apr 2015 14:38:37 -0400 Subject: [PATCH 2/3] Reorder methods for reasons of pedantry --- src/Client.php | 24 ++++++------- src/Model/CollectionInfo.php | 40 ++++++++++----------- src/Model/CollectionInfoLegacyIterator.php | 42 +++++++++++----------- src/Model/DatabaseInfo.php | 22 ++++++------ src/Model/IndexInfo.php | 22 ++++++------ src/Model/IndexInput.php | 18 +++++----- 6 files changed, 84 insertions(+), 84 deletions(-) diff --git a/src/Client.php b/src/Client.php index 6ed200957..8386addf3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -79,44 +79,44 @@ public function listDatabases() } /** - * Select a database. + * Select a collection. * * If a write concern or read preference is not specified, the write concern * or read preference of the Client will be applied, respectively. * - * @param string $databaseName Name of the database to select + * @param string $databaseName Name of the database containing the collection + * @param string $collectionName Name of the collection to select * @param WriteConcern $writeConcern Default write concern to apply * @param ReadPreference $readPreference Default read preference to apply - * @return Database + * @return Collection */ - public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) + public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) { + $namespace = $databaseName . '.' . $collectionName; // TODO: inherit from Manager options once PHPC-196 is implemented $writeConcern = $writeConcern ?: $this->writeConcern; $readPreference = $readPreference ?: $this->readPreference; - return new Database($this->manager, $databaseName, $writeConcern, $readPreference); + return new Collection($this->manager, $namespace, $writeConcern, $readPreference); } /** - * Select a collection. + * Select a database. * * If a write concern or read preference is not specified, the write concern * or read preference of the Client will be applied, respectively. * - * @param string $databaseName Name of the database containing the collection - * @param string $collectionName Name of the collection to select + * @param string $databaseName Name of the database to select * @param WriteConcern $writeConcern Default write concern to apply * @param ReadPreference $readPreference Default read preference to apply - * @return Collection + * @return Database */ - public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) + public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null) { - $namespace = $databaseName . '.' . $collectionName; // TODO: inherit from Manager options once PHPC-196 is implemented $writeConcern = $writeConcern ?: $this->writeConcern; $readPreference = $readPreference ?: $this->readPreference; - return new Collection($this->manager, $namespace, $writeConcern, $readPreference); + return new Database($this->manager, $databaseName, $writeConcern, $readPreference); } } diff --git a/src/Model/CollectionInfo.php b/src/Model/CollectionInfo.php index 15e222b45..0aefc8f29 100644 --- a/src/Model/CollectionInfo.php +++ b/src/Model/CollectionInfo.php @@ -28,53 +28,53 @@ public function __construct(array $info) } /** - * Return the collection name. + * Return the maximum number of documents to keep in the capped collection. * - * @return string + * @return integer|null */ - public function getName() + public function getCappedMax() { - return (string) $this->info['name']; + return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null; } /** - * Return the collection options. + * Return the maximum size (in bytes) of the capped collection. * - * @return array + * @return integer|null */ - public function getOptions() + public function getCappedSize() { - return isset($this->info['options']) ? (array) $this->info['options'] : array(); + return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null; } /** - * Return whether the collection is a capped collection. + * Return the collection name. * - * @return boolean + * @return string */ - public function isCapped() + public function getName() { - return ! empty($this->info['options']['capped']); + return (string) $this->info['name']; } /** - * Return the maximum number of documents to keep in the capped collection. + * Return the collection options. * - * @return integer|null + * @return array */ - public function getCappedMax() + public function getOptions() { - return isset($this->info['options']['max']) ? (integer) $this->info['options']['max'] : null; + return isset($this->info['options']) ? (array) $this->info['options'] : array(); } /** - * Return the maximum size (in bytes) of the capped collection. + * Return whether the collection is a capped collection. * - * @return integer|null + * @return boolean */ - public function getCappedSize() + public function isCapped() { - return isset($this->info['options']['size']) ? (integer) $this->info['options']['size'] : null; + return ! empty($this->info['options']['capped']); } /** diff --git a/src/Model/CollectionInfoLegacyIterator.php b/src/Model/CollectionInfoLegacyIterator.php index 01a6a33df..b1a5972b3 100644 --- a/src/Model/CollectionInfoLegacyIterator.php +++ b/src/Model/CollectionInfoLegacyIterator.php @@ -40,27 +40,6 @@ public function __construct(Traversable $iterator) parent::__construct($iterator); } - /** - * Return the current element as a CollectionInfo instance. - * - * @see CollectionInfoIterator::current() - * @see http://php.net/iterator.current - * @return CollectionInfo - */ - public function current() - { - $info = parent::current(); - - // Trim the database prefix up to and including the first dot - $firstDot = strpos($info['name'], '.'); - - if ($firstDot !== false) { - $info['name'] = (string) substr($info['name'], $firstDot + 1); - } - - return new CollectionInfo($info); - } - /** * Filter out internal or invalid collections. * @@ -92,4 +71,25 @@ public function accept() return true; } + + /** + * Return the current element as a CollectionInfo instance. + * + * @see CollectionInfoIterator::current() + * @see http://php.net/iterator.current + * @return CollectionInfo + */ + public function current() + { + $info = parent::current(); + + // Trim the database prefix up to and including the first dot + $firstDot = strpos($info['name'], '.'); + + if ($firstDot !== false) { + $info['name'] = (string) substr($info['name'], $firstDot + 1); + } + + return new CollectionInfo($info); + } } diff --git a/src/Model/DatabaseInfo.php b/src/Model/DatabaseInfo.php index 18e0b9574..61d26194a 100644 --- a/src/Model/DatabaseInfo.php +++ b/src/Model/DatabaseInfo.php @@ -26,6 +26,17 @@ public function __construct(array $info) $this->info = $info; } + /** + * Return the collection info as an array. + * + * @see http://php.net/oop5.magic#language.oop5.magic.debuginfo + * @return array + */ + public function __debugInfo() + { + return $this->info; + } + /** * Return the database name. * @@ -55,15 +66,4 @@ public function isEmpty() { return (boolean) $this->info['empty']; } - - /** - * Return the collection info as an array. - * - * @see http://php.net/oop5.magic#language.oop5.magic.debuginfo - * @return array - */ - public function __debugInfo() - { - return $this->info; - } } diff --git a/src/Model/IndexInfo.php b/src/Model/IndexInfo.php index 6bc26a1c0..02e4da497 100644 --- a/src/Model/IndexInfo.php +++ b/src/Model/IndexInfo.php @@ -34,6 +34,17 @@ public function __construct(array $info) $this->info = $info; } + /** + * Return the collection info as an array. + * + * @see http://php.net/oop5.magic#language.oop5.magic.debuginfo + * @return array + */ + public function __debugInfo() + { + return $this->info; + } + /** * Return the index key. * @@ -157,15 +168,4 @@ public function offsetUnset($key) { throw new BadMethodCallException('IndexInfo is immutable'); } - - /** - * Return the collection info as an array. - * - * @see http://php.net/oop5.magic#language.oop5.magic.debuginfo - * @return array - */ - public function __debugInfo() - { - return $this->info; - } } diff --git a/src/Model/IndexInput.php b/src/Model/IndexInput.php index 023b56d53..dc6338f7e 100644 --- a/src/Model/IndexInput.php +++ b/src/Model/IndexInput.php @@ -61,24 +61,24 @@ public function __construct(array $index) } /** - * Serialize the index information to BSON for index creation. + * Return the index name. * - * @see MongoDB\Collection::createIndexes() - * @see http://php.net/bson-serializable.bsonserialize + * @param string */ - public function bsonSerialize() + public function __toString() { - return $this->index; + return $this->index['name']; } /** - * Return the index name. + * Serialize the index information to BSON for index creation. * - * @param string + * @see MongoDB\Collection::createIndexes() + * @see http://php.net/bson-serializable.bsonserialize */ - public function __toString() + public function bsonSerialize() { - return $this->index['name']; + return $this->index; } /** From 869c94a1a21644159e2b42cee0188e1f59cc3a6f Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 27 Apr 2015 14:42:27 -0400 Subject: [PATCH 3/3] Pedantic method declaration test should ignore inherited methods --- tests/PedantryTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/PedantryTest.php b/tests/PedantryTest.php index 46dc96715..5ee136c62 100644 --- a/tests/PedantryTest.php +++ b/tests/PedantryTest.php @@ -21,6 +21,13 @@ public function testMethodsAreOrderedAlphabeticallyByVisibility($className) $class = new ReflectionClass($className); $methods = $class->getMethods(); + $methods = array_filter( + $methods, + function(ReflectionMethod $method) use ($class) { + return $method->getDeclaringClass() == $class; + } + ); + $getSortValue = function(ReflectionMethod $method) { if ($method->getModifiers() & ReflectionMethod::IS_PRIVATE) { return '2' . $method->getName();