private function validateVariableScopes( $taxonomy, $variableSetInstance )
	{
		$scopeArcs = $taxonomy->getGenericArc( XBRL_Constants::$arcRoleVariablesScope, $variableSetInstance->extendedLinkRoleUri, null, $variableSetInstance->path, null, $variableSetInstance->linkbase );

		if ( ! $scopeArcs ) return true;

		// Include any scope variables referenced directly from this variable set or indirectly because
		// because the variable referenced by this variable set references another variable set and so on
		$label = $variableSetInstance->label;
		$labels = array();
		while (true)
		{
			$found = false;
			$labels[] = $label;
			foreach ( $scopeArcs as $scopeArc )
			{
				if ( ! isset( $scopeArc['to'] ) || $scopeArc['to'] != $label ) continue;
				// This is a different kind of variable
				$scopeQName = new QName( $scopeArc['name']['originalPrefix'], $scopeArc['name']['namespace'], $scopeArc['name']['name'] );
				$found = true;
				break;
			}

			if ( ! $found ) break;
			if ( isset( $variableSetInstance->scopeDependencies[ $scopeQName->clarkNotation() ] ) )
			{
				// Cyclic relationship detected
				$this->log->formula_validation( "Scope variables", "The scope variables include a cyclic dependency",
					array(
						'error' => 'xbrl21:directedCycleError',
						'variable sets' => implode( ", ", $labels ) . ", ..."
					)
				);
			}

			$variableSetInstance->scopeDependencies[ $scopeQName->clarkNotation() ] = $scopeArc;
			if ( ! isset( $scopeArc['from'] ) ) break;
			$label = $scopeArc['from'];
		}

		// Now add any variables from the source variable set so they can be included in this variable set's validation
		foreach ( $variableSetInstance->scopeDependencies as $qname => $scopeDependency )
		{
			$fromVariableSetKey = "{$scopeDependency['fromRoleUri']}#{$scopeDependency['from']}";

			$scopeVariableInstances = $this->variableSets[ $fromVariableSetKey ];
			// look for any variables
			foreach ( $scopeVariableInstances as $scopeVariableInstanceKey => $scopeVariableInstance )
			{
				foreach ( $scopeVariableInstance->variablesByQName as $variableQName => $variable )
				{
					// Create a dummy variable. It will be overridden later on.
					if ( isset( $variableSetInstance->variablesByQName[ $variableQName ] ) ) continue;
					$variableSetInstance->variablesByQName[ $variableQName ]  = new ScopeVariable();
				}
			}
		}

		return true;
	}