Services: Convert PermissionManager's static to a const now HHVM is gone
[lhc/web/wiklou.git] / includes / config / ServiceOptions.php
index 0f3743f..46f6689 100644 (file)
@@ -13,16 +13,17 @@ use Wikimedia\Assert\Assert;
  * objects).
  *
  * Services that take this type as a parameter to their constructor should specify a list of the
- * keys they expect to receive in an array. The convention is to make it a public static variable
- * called $constructorOptions. (When we drop HHVM support -- see T192166 -- it should become a
- * const.) In the constructor, they should call assertRequiredOptions() to make sure that they
- * weren't passed too few or too many options. This way it's clear what each class depends on, and
- * that it's getting passed the correct set of options. (This means there are no optional options.
- * This makes sense for services, since they shouldn't be constructed by outside code.)
+ * keys they expect to receive in an array. The convention is to make it a public const called
+ * CONSTRUCTOR_OPTIONS. In the constructor, they should call assertRequiredOptions() to make sure
+ * that they weren't passed too few or too many options. This way it's clear what each class
+ * depends on, and that it's getting passed the correct set of options. (This means there are no
+ * optional options. This makes sense for services, since they shouldn't be constructed by
+ * outside code.)
  *
  * @since 1.34
  */
 class ServiceOptions {
+       private $keys = [];
        private $options = [];
 
        /**
@@ -33,6 +34,7 @@ class ServiceOptions {
         * @throws InvalidArgumentException if one of $keys is not found in any of $sources
         */
        public function __construct( array $keys, ...$sources ) {
+               $this->keys = $keys;
                foreach ( $keys as $key ) {
                        foreach ( $sources as $source ) {
                                if ( $source instanceof Config ) {
@@ -58,20 +60,21 @@ class ServiceOptions {
         * @param string[] $expectedKeys
         */
        public function assertRequiredOptions( array $expectedKeys ) {
-               $actualKeys = array_keys( $this->options );
-               $extraKeys = array_diff( $actualKeys, $expectedKeys );
-               $missingKeys = array_diff( $expectedKeys, $actualKeys );
-               Assert::precondition( !$extraKeys && !$missingKeys,
-                       (
-                       $extraKeys
-                               ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
-                               : ''
-                       ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
-                       $missingKeys
-                               ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
-                               : ''
-                       )
-               );
+               if ( $this->keys !== $expectedKeys ) {
+                       $extraKeys = array_diff( $this->keys, $expectedKeys );
+                       $missingKeys = array_diff( $expectedKeys, $this->keys );
+                       Assert::precondition( !$extraKeys && !$missingKeys,
+                               (
+                               $extraKeys
+                                       ? 'Unsupported options passed: ' . implode( ', ', $extraKeys ) . '!'
+                                       : ''
+                               ) . ( $extraKeys && $missingKeys ? ' ' : '' ) . (
+                               $missingKeys
+                                       ? 'Required options missing: ' . implode( ', ', $missingKeys ) . '!'
+                                       : ''
+                               )
+                       );
+               }
        }
 
        /**