/** * @param string $dataTypeId * * @throws OutOfBoundsException * @return ValueValidator[] */ public function getValidators($dataTypeId) { if (!isset($this->validatorBuilders[$dataTypeId])) { // NOTE: fail hard, to avoid bypassing validators if the data type is mistyped or some such. throw new OutOfBoundsException('No validators known for data type ' . $dataTypeId); } $validators = call_user_func($this->validatorBuilders[$dataTypeId]); Assert::postcondition(is_array($validators), "Factory function for {$dataTypeId} did not return an array of ValueValidator objects."); foreach ($validators as $v) { Assert::postcondition($v instanceof ValueValidator, "Factory function for {$dataTypeId} did not return an array of ValueValidator objects."); } return $validators; }
public function testPostcondition_fail() { $this->setExpectedException('Wikimedia\\Assert\\PostconditionException'); Assert::postcondition(false, 'test'); }
/** * @param array $wiringFiles A list of PHP files to load wiring information from. * Each file is loaded using PHP's include mechanism. Each file is expected to * return an associative array that maps service names to instantiator functions. */ public function loadWiringFiles(array $wiringFiles) { foreach ($wiringFiles as $file) { // the wiring file is required to return an array of instantiators. $wiring = (require $file); Assert::postcondition(is_array($wiring), "Wiring file {$file} is expected to return an array!"); $this->applyWiring($wiring); } }
/** * Remove the given $elt from the BalanceStack, optionally * flattening it in the process. * @param BalanceElement $elt The element to remove. * @param bool $flatten Whether to flatten the removed element. */ public function removeElement(BalanceElement $elt, $flatten = true) { Assert::parameter($elt->parent !== 'flat', '$elt', '$elt should not already have been flattened.'); Assert::parameter($elt->parent->parent !== 'flat', '$elt', 'The parent of $elt should not already have been flattened.'); $idx = array_search($elt, $this->elements, true); Assert::parameter($idx !== false, '$elt', 'must be in stack'); array_splice($this->elements, $idx, 1); if ($idx === count($this->elements)) { $this->currentNode = $this->elements[$idx - 1]; } if ($flatten) { // serialize $elt into its parent // otherwise, it will eventually serialize when the parent // is serialized, we just hold onto the memory for its // tree of objects a little longer. $elt->flatten($this->config); } Assert::postcondition(array_search($elt, $this->elements, true) === false, '$elt should no longer be in open elements stack'); }
/** * Instantiate SnakFormatters based on the constructor callbacks passed to the constructor. * * @param string $format * @param FormatterOptions $options * * @return SnakFormatter[] */ private function createSnakFormatters($format, FormatterOptions $options) { $formatters = array(); foreach ($this->snakFormatterConstructorCallbacks as $key => $callback) { $instance = call_user_func($callback, $format, $options); Assert::postcondition($instance instanceof SnakFormatter, "Constructor callback did not return a SnakFormatter for {$key}"); $formatters[$key] = $instance; } return $formatters; }
/** * Instantiates the formatters defined for the given format in * WikibaseValueFormatterBuilders::$valueFormatterSpecs. * * @see WikibaseValueFormatterBuilders::$valueFormatterSpecs * * @param string $format One of the SnakFormatter::FORMAT_... constants. * @param FormatterOptions $options * * @return ValueFormatter[] A map from prefixed type IDs to ValueFormatter instances. */ private function buildDefinedFormatters($format, FormatterOptions $options) { $formatters = array(); foreach ($this->factoryFunctions as $type => $func) { $formatter = call_user_func($func, $format, $options); Assert::postcondition($formatter instanceof ValueFormatter, "Callback for {$type} did not return a ValueFormatter"); $formatters[$type] = $formatter; } return $formatters; }
/** * Returns a human readable comment representing the change. * * @since 0.4 * * @param EntityChange $change the change to get a comment for * @param Title|null $target The page we create an edit summary for. Needed to create an article * specific edit summary on site link changes. Ignored otherwise. * * @throws MWException * @return string */ private function getEditComment(EntityChange $change, Title $target = null) { $siteLinkDiff = $change instanceof ItemChange ? $change->getSiteLinkDiff() : null; $editComment = ''; if ($siteLinkDiff !== null && !$siteLinkDiff->isEmpty()) { $action = $change->getAction(); $siteLinkComment = $this->siteLinkCommentCreator->getEditComment($siteLinkDiff, $action, $target); $editComment = $siteLinkComment === null ? '' : $siteLinkComment; } if ($editComment === '') { $editComment = $change->getComment(); } if ($editComment === '') { // If there is no comment, use something generic. This shouldn't happen. wfWarn('Failed to find edit comment for EntityChange'); $editComment = $this->msg('wikibase-comment-update')->text(); } Assert::postcondition(is_string($editComment), '$editComment must be a string'); return $editComment; }