Find the first occurrence of a given element inside the content (single level)

@param string $fqcn Fully Qualified Class Name, e.g. ResourceElement::class
@return ApiElement|null
350694
public function getFirstElementByType($fqcn)
    {
        $match = null;

        foreach ($this->getContent() as $element) {
            if (!($element instanceof $fqcn)) {
                continue;
            }

            $match = $element;
            break;
        }

        return $match;
    }
1178831
public function createDefinitionFromNode(Node $node, string $fqn = null): Definition
    {
        $def = new Definition;
        $def->fqn = $fqn;

        // Determines whether the suggestion will show after "new"
        $def->canBeInstantiated = (
            $node instanceof Node\Statement\ClassDeclaration &&
            // check whether it is not an abstract class
            ($node->abstractOrFinalModifier === null || $node->abstractOrFinalModifier->kind !== PhpParser\TokenKind::AbstractKeyword)
        );

        // Interfaces, classes, traits, namespaces, functions, and global const elements
        $def->isMember = !(
            $node instanceof PhpParser\ClassLike ||

            ($node instanceof Node\Statement\NamespaceDefinition && $node->name !== null) ||

            $node instanceof Node\Statement\FunctionDeclaration ||

            ($node instanceof Node\ConstElement && $node->parent->parent instanceof Node\Statement\ConstDeclaration)
        );

        // Definition is affected by global namespace fallback if it is a global constant or a global function
        $def->roamed = (
            $fqn !== null
            && strpos($fqn, '\\') === false
            && (
                ($node instanceof Node\ConstElement && $node->parent->parent instanceof Node\Statement\ConstDeclaration)
                || $node instanceof Node\Statement\FunctionDeclaration
            )
        );

        // Static methods and static property declarations
        $def->isStatic = (
            ($node instanceof Node\MethodDeclaration && $node->isStatic()) ||

            (($propertyDeclaration = ParserHelpers\tryGetPropertyDeclaration($node)) !== null
            && $propertyDeclaration->isStatic())
        );

        if ($node instanceof Node\Statement\ClassDeclaration &&
            // TODO - this should be better represented in the parser API
            $node->classBaseClause !== null && $node->classBaseClause->baseClass !== null) {
            $def->extends = [(string)$node->classBaseClause->baseClass->getResolvedName()];
        } elseif (
            $node instanceof Node\Statement\InterfaceDeclaration &&
            // TODO - this should be better represented in the parser API
            $node->interfaceBaseClause !== null && $node->interfaceBaseClause->interfaceNameList !== null
        ) {
            $def->extends = [];
            foreach ($node->interfaceBaseClause->interfaceNameList->getValues() as $n) {
                $def->extends[] = (string)$n->getResolvedName();
            }
        }

        $def->symbolInformation = SymbolInformationFactory::fromNode($node, $fqn);

        if ($def->symbolInformation !== null) {
            $def->type = $this->getTypeFromNode($node);
            $def->declarationLine = $this->getDeclarationLineFromNode($node);
            $def->documentation = $this->getDocumentationFromNode($node);
        }

        if ($node instanceof FunctionLike) {
            $def->signatureInformation = $this->signatureInformationFactory->create($node);
        }

        return $def;
    }
25045
public function findFirst($key,$value)
    {
        $result = $this->findItems(
            function (ClientObject $item) use ($key, $value) {
                return $item->getProperty($key) === $value;
            });
        return ((count($result) > 0) ? array_values($result)[0] : null);
    }
900610
public static Element getFirstChildElement(Node node) {
    	
    	if(node instanceof NestableNode) {
    		return ((NestableNode) node).getFirstElementChild();
    	}
        
        return null;
    }
910460
private static function getDocumentedElement($fqsen)
    {
        $projectDescriptor = self::$descriptorBuilder->getProjectDescriptor();
        $elementList = $projectDescriptor->getIndexes()->get('elements');
        $prefixedLink = '~\\' . $fqsen;

        if (isset($elementList[$fqsen])) {
            return $elementList[$fqsen];
        } elseif (isset($elementList[$prefixedLink])) {
            return $elementList[$prefixedLink];
        }

        return false;
    }
385658
private static function getContainingClassFqn(Node $node)
    {
        $classNode = $node->getFirstAncestor(Node\Statement\ClassDeclaration::class);
        if ($classNode === null) {
            return null;
        }
        return (string)$classNode->getNamespacedName();
    }
272668
public function getReflectionClass($classFQN)
    {
        try {
            $reflectionClass = new \ReflectionClass($classFQN);
        } catch (\ReflectionException $re) {
            return new CoverFishMessageError(CoverFishMessageError::PHPUNIT_REFLECTION_CLASS_NOT_FOUND, $classFQN);
        }

        return $reflectionClass;
    }
338963
public static function getDefinedFqn($node)
    {
        $parent = $node->parent;
        // Anonymous classes don't count as a definition
        // INPUT                    OUTPUT:
        // namespace A\B;
        // class C { }              A\B\C
        // interface C { }          A\B\C
        // trait C { }              A\B\C
        if (
            $node instanceof PhpParser\ClassLike
        ) {
            $className = (string)$node->getNamespacedName();
            // An (invalid) class declaration without a name will have an empty string as name,
            // but should not define an FQN
            if ($className === '') {
                return null;
            }
            return $className;
        }

        // INPUT                   OUTPUT:
        // namespace A\B;           A\B
        if ($node instanceof Node\Statement\NamespaceDefinition && $node->name instanceof Node\QualifiedName) {
            $name = (string) PhpParser\ResolvedName::buildName($node->name->nameParts, $node->getFileContents());
            return $name === "" ? null : $name;
        }

        // INPUT                   OUTPUT:
        // namespace A\B;
        // function a();           A\B\a();
        if ($node instanceof Node\Statement\FunctionDeclaration) {
            // Function: use functionName() as the name
            $name = (string)$node->getNamespacedName();
            return $name === "" ? null : $name . '()';
        }

        // INPUT                        OUTPUT
        // namespace A\B;
        // class C {
        //   function a () {}           A\B\C->a()
        //   static function b() {}     A\B\C::b()
        // }
        if ($node instanceof Node\MethodDeclaration) {
            // Class method: use ClassName->methodName() as name
            $class = $node->getFirstAncestor(
                Node\Expression\ObjectCreationExpression::class,
                PhpParser\ClassLike::class
            );
            if (!isset($class->name)) {
                // Ignore anonymous classes
                return null;
            }
            if ($node->isStatic()) {
                return (string)$class->getNamespacedName() . '::' . $node->getName() . '()';
            } else {
                return (string)$class->getNamespacedName() . '->' . $node->getName() . '()';
            }
        }

        // INPUT                        OUTPUT
        // namespace A\B;
        // class C {
        //   static $a = 4, $b = 4      A\B\C::$a, A\B\C::$b
        //   $a = 4, $b = 4             A\B\C->$a, A\B\C->$b // TODO verify variable name
        // }
        if (
            ($propertyDeclaration = ParserHelpers\tryGetPropertyDeclaration($node)) !== null &&
            ($classDeclaration =
                $node->getFirstAncestor(
                    Node\Expression\ObjectCreationExpression::class,
                    PhpParser\ClassLike::class
                )
            ) !== null && isset($classDeclaration->name)) {
            $name = $node->getName();
            if ($propertyDeclaration->isStatic()) {
                // Static Property: use ClassName::$propertyName as name
                return (string)$classDeclaration->getNamespacedName() . '::$' . $name;
            }

            // Instance Property: use ClassName->propertyName as name
            return (string)$classDeclaration->getNamespacedName() . '->' . $name;
        }

        // INPUT                        OUTPUT
        // namespace A\B;
        // const FOO = 5;               A\B\FOO
        // class C {
        //   const $a, $b = 4           A\B\C::$a(), A\B\C::$b
        // }
        if (($constDeclaration = ParserHelpers\tryGetConstOrClassConstDeclaration($node)) !== null) {
            if ($constDeclaration instanceof Node\Statement\ConstDeclaration) {
                // Basic constant: use CONSTANT_NAME as name
                return (string)$node->getNamespacedName();
            }

            // Class constant: use ClassName::CONSTANT_NAME as name
            $classDeclaration = $constDeclaration->getFirstAncestor(
                Node\Expression\ObjectCreationExpression::class,
                PhpParser\ClassLike::class
            );

            if (!isset($classDeclaration->name)) {
                return null;
            }
            return (string)$classDeclaration->getNamespacedName() . '::' . $node->getName();
        }

        if (ParserHelpers\isConstDefineExpression($node)) {
            return $node->argumentExpressionList->children[0]->expression->getStringContentsText();
        }

        return null;
    }
1287094
public function firstElementGet()
    {
        $keys = array_keys( $this->collection );
        if( $keys ) {
            return $this->collection[array_shift($keys)];
        }
        return null;
    }
1174366
public function getFirstChild($sortColumn = 'cDisplayOrder asc', $excludeSystemPages = false)
    {
        if ($excludeSystemPages) {
            $systemPages = ' and cIsSystemPage = 0';
        } else {
            $systemPages = '';
        }

        $db = Database::connection();
        $cID = $db->fetchColumn('select Pages.cID from Pages inner join CollectionVersions on Pages.cID = CollectionVersions.cID where cvIsApproved = 1 and cParentID = ? '.$systemPages." order by {$sortColumn}", array($this->cID));
        if ($cID > 1) {
            return static::getByID($cID, 'ACTIVE');
        }

        return false;
    }
270503
public function getNamespaceRootToDirectoryFromFqn(string $fqn, string $directory): ?string
    {
        $strPos = \strrpos(
            $fqn,
            $directory
        );
        if (false !== $strPos) {
            return $this->tidy(\substr($fqn, 0, $strPos + \strlen($directory)));
        }

        return null;
    }
1219377
public function isFirst()
    {
        $node = $this->node;
        return $node->index === 'content'
            || isset($node->position) && $node->position === 1;
    }
363446
public static function fromQName( $qn )
	{
		$qname = new QNameValue();
		$qname->LocalName = $qn->localName;
		$qname->Prefix = $qn->prefix;
		$qname->NamespaceUri = $qn->namespaceURI;

		if ( strlen( $qn->localName ) == 0 ||
			 ! preg_match( "/^" . NameValue::$ncName . "$/u", $qn->localName ) ||
			 ( strlen( $qn->prefix ) != 0 && ! preg_match( "/^" . NameValue::$ncName . "$/u", $qn->prefix ) )
		)
		{
			throw XPath2Exception::withErrorCodeAndParam( "FOCA0002", Resources::FOCA0002, $qn->__toString() );
		}

		return $qname;
	}
234791
private final static function getInstance($fqcn, $name)
    {
        $instances = self::getInstances($fqcn);

        if (!isset($instances[$name])) {
            return null;
        } else {
            return $instances[$name];
        }
    }
196888
private final static function &getInstances($fqcn)
    {
        if (!isset(self::$instances[$fqcn])) {
            self::loadClass($fqcn);
        }

        return self::$instances[$fqcn];
    }
460323
protected function fetchElementByFqsen($fqsen)
    {
        return isset($this->elementList[$fqsen]) ? $this->elementList[$fqsen] : null;
    }
1172
private function getContainingClassType(Node $node)
    {
        $classFqn = $this->getContainingClassFqn($node);
        return $classFqn ? new Types\Object_(new Fqsen('\\' . $classFqn)) : null;
    }