Skip to content

Commit

Permalink
Add a bunch of types
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw authored and malberts committed Nov 8, 2021
1 parent f610f5e commit b0d4465
Showing 1 changed file with 12 additions and 43 deletions.
55 changes: 12 additions & 43 deletions src/ComponentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
class ComponentFactory {

// the root component of the page; should be of type Container
private $mRootComponent = null;
private ?Container $rootComponent = null;

private string $layoutFileName;

/** @var QuickTemplate|null */
private $skinTemplate;
private ?QuickTemplate $skinTemplate;

private HookContainer $hookContainer;
private FileFetcher $fileFetcher;
Expand All @@ -62,11 +61,10 @@ public function __construct( string $layoutFileName, HookContainer $hookContaine
}

/**
* @return Container
* @throws MWException
*/
public function getRootComponent() {
if ( $this->mRootComponent === null ) {
public function getRootComponent(): Container {
if ( $this->rootComponent === null ) {

$document = $this->getDomDocument();

Expand All @@ -76,7 +74,7 @@ public function getRootComponent() {

$element = $roots->item( 0 );
if ( $element instanceof DOMElement ) {
$this->mRootComponent = $this->getComponent( $element );
$this->rootComponent = $this->getComponent( $element );
}

} else {
Expand All @@ -86,7 +84,7 @@ public function getRootComponent() {
}
}

return $this->mRootComponent;
return $this->rootComponent;
}

private function getDomDocument(): DOMDocument {
Expand Down Expand Up @@ -114,14 +112,9 @@ private function setLayoutFileName( string $fileName ) {
}

/**
* @param DOMElement $description
* @param int $indent
* @param string $htmlClassAttribute
*
* @throws MWException
* @return Container
*/
public function getComponent( DOMElement $description, $indent = 0, $htmlClassAttribute = '' ) {
public function getComponent( DOMElement $description, int $indent = 0, string $htmlClassAttribute = '' ): Container {
$className = $this->getComponentClassName( $description );
$component = new $className( $this->getSkinTemplate(), $description, $indent,
$htmlClassAttribute );
Expand All @@ -138,13 +131,10 @@ public function getComponent( DOMElement $description, $indent = 0, $htmlClassAt
}

/**
* @param DOMElement $description
*
* @return string
* @throws MWException
* @since 1.1
*/
protected function getComponentClassName( DOMElement $description ) {
protected function getComponentClassName( DOMElement $description ): string {
$className = $this->mapDescriptionToClassName( $description );

if ( !class_exists( $className ) ||
Expand All @@ -157,12 +147,9 @@ protected function getComponentClassName( DOMElement $description ) {
}

/**
* @param DOMElement $description
*
* @return string
* @throws MWException
*/
protected function mapDescriptionToClassName( DOMElement $description ) {
protected function mapDescriptionToClassName( DOMElement $description ): string {
$nodeName = strtolower( $description->nodeName );

$mapOfComponentsToClassNames = [
Expand All @@ -185,24 +172,15 @@ protected function mapDescriptionToClassName( DOMElement $description ) {
$this->layoutFileName, $description->getLineNo(), $description->nodeName ) );
}

/**
* @return QuickTemplate|null
*/
public function getSkinTemplate() {
public function getSkinTemplate(): ?QuickTemplate {
return $this->skinTemplate;
}

/**
* @param QuickTemplate $skinTemplate
*/
public function setSkinTemplate( QuickTemplate $skinTemplate ) {
$this->skinTemplate = $skinTemplate;
}

/**
* @param DOMElement $description
* @param Component $component
*
* @return mixed
* @throws MWException
*/
Expand All @@ -226,20 +204,11 @@ protected function getModifiedComponent( DOMElement $description, Component $com
return new $className( $component, $description );
}

/**
* @param string $fileName
*
* @return string
*/
public function sanitizeFileName( $fileName ) {
public function sanitizeFileName( string $fileName ): string {
return str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $fileName );
}

/**
* @param DOMElement $description
* @return string
*/
protected function mapComponentDescriptionToClassName( DOMElement $description ) {
protected function mapComponentDescriptionToClassName( DOMElement $description ): string {
if ( $description->hasAttribute( 'type' ) ) {
$className = $description->getAttribute( 'type' );
$parent = $description->parentNode;
Expand Down

0 comments on commit b0d4465

Please sign in to comment.