Add return type `static` for `@return $this`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / database / table / index / DatabaseTableIndex.class.php
index 746d7e8c20def535210b17a6bfd5cebf810e4294..b2cda9355ffffd33c587863ca5d2f057ea5dc707 100644 (file)
 <?php
+
 namespace wcf\system\database\table\index;
+
 use wcf\system\database\table\TDroppableDatabaseComponent;
 
 /**
  * Represents an index of a database table.
- * 
- * @author     Matthias Schmidt
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @package    WoltLabSuite\Core\System\Database\Table\Index
- * @since      5.2
+ *
+ * @author  Matthias Schmidt
+ * @copyright   2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Database\Table\Index
+ * @since   5.2
  */
-class DatabaseTableIndex {
-       use TDroppableDatabaseComponent;
-       
-       /**
-        * indexed columns
-        * @var string[]
-        */
-       protected $columns;
-       
-       /**
-        * is `true` if index name has been automatically generated
-        * @var bool
-        */
-       protected $generatedName = false;
-       
-       /**
-        * name of index
-        * @var string
-        */
-       protected $name;
-       
-       /**
-        * type of index (see `*_TYPE` constants)
-        * @var null|string
-        */
-       protected $type;
-       
-       const DEFAULT_TYPE = null;
-       const PRIMARY_TYPE = 'PRIMARY';
-       const UNIQUE_TYPE = 'UNIQUE';
-       const FULLTEXT_TYPE = 'FULLTEXT';
-       
-       /**
-        * Creates a new `DatabaseTableIndex` object.
-        * 
-        * @param       string          $name           column name
-        */
-       protected function __construct($name) {
-               $this->name = $name;
-       }
-       
-       /**
-        * Sets the indexed columns and returns the index.
-        * 
-        * @param       string[]        $columns        indexed columns
-        * @return      $this                           this index
-        */
-       public function columns($columns) {
-               $this->columns = array_values($columns);
-               
-               return $this;
-       }
-
-       /**
-        * Sets the automatically generated name of the index.
-        * 
-        * @param       string          $name           index name
-        * @return      $this                           this index
-        */
-       public function generatedName($name) {
-               $this->name($name);
-               $this->generatedName = true;
-               
-               return $this;
-       }
-       
-       /**
-        * Returns the index columns.
-        * 
-        * @return      string[]
-        */
-       public function getColumns() {
-               return $this->columns;
-       }
-       
-       /**
-        * Returns the data used by `DatabaseEditor` to add the index to a table.
-        * 
-        * @return      array
-        */
-       public function getData() {
-               return [
-                       'columns' => implode(',', $this->columns),
-                       'type' => $this->type
-               ];
-       }
-       
-       /**
-        * Returns the name of the index.
-        * 
-        * @return      string
-        */
-       public function getName() {
-               return $this->name;
-       }
-       
-       /**
-        * Returns the type of the index (see `*_TYPE` constants).
-        * 
-        * @return      null|string
-        */
-       public function getType() {
-               return $this->type;
-       }
-       
-       /**
-        * Returns `true` if the name of the index has been automatically generated.
-        * 
-        * @return      bool
-        */
-       public function hasGeneratedName() {
-               return $this->generatedName;
-       }
-       
-       /**
-        * Sets the name of the index.
-        * 
-        * @param       string          $name           index name
-        * @return      $this                           this index
-        */
-       public function name($name) {
-               $this->name = $name;
-               
-               return $this;
-       }
-       
-       /**
-        * Sets the type of the index and returns the index
-        * 
-        * @param       null|string     $type           index type
-        * @return      $this                           this index
-        * @throws      \InvalidArgumentException       if given type is invalid
-        */
-       public function type($type) {
-               if ($type !== static::DEFAULT_TYPE && $type !== static::PRIMARY_TYPE && $type !== static::UNIQUE_TYPE && $type !== static::FULLTEXT_TYPE) {
-                       throw new \InvalidArgumentException("Unknown index type '{$type}'.");
-               }
-               
-               $this->type = $type;
-               
-               return $this;
-       }
-       
-       /**
-        * Returns a `DatabaseTableIndex` object with the given name.
-        * 
-        * @param       string          $name
-        * @return      static
-        */
-       public static function create($name = '') {
-               return new static($name);
-       }
-       
-       /**
-        * Returns a `DatabaseTableIndex` object with the given name and data.
-        * 
-        * @param       string          $name
-        * @param       array           $data           data returned by `DatabaseEditor::getIndexInformation()`
-        * @return      static
-        */
-       public static function createFromData($name, array $data) {
-               return static::create($name)
-                       ->type($data['type'])
-                       ->columns($data['columns']);
-       }
+final class DatabaseTableIndex
+{
+    use TDroppableDatabaseComponent;
+
+    /**
+     * indexed columns
+     * @var string[]
+     */
+    private array $columns;
+
+    /**
+     * is `true` if index name has been automatically generated
+     */
+    private bool $generatedName = false;
+
+    /**
+     * name of index
+     */
+    private string $name;
+
+    /**
+     * type of index (see `*_TYPE` constants)
+     */
+    private ?string $type = null;
+
+    const DEFAULT_TYPE = null;
+
+    const PRIMARY_TYPE = 'PRIMARY';
+
+    const UNIQUE_TYPE = 'UNIQUE';
+
+    const FULLTEXT_TYPE = 'FULLTEXT';
+
+    /**
+     * Creates a new `DatabaseTableIndex` object.
+     */
+    private function __construct(string $name)
+    {
+        $this->name = $name;
+    }
+
+    /**
+     * Sets the indexed columns and returns the index.
+     *
+     * @param string[] $columns indexed columns
+     * @return  $this
+     */
+    public function columns(array $columns): static
+    {
+        $this->columns = \array_values($columns);
+
+        return $this;
+    }
+
+    /**
+     * Sets the automatically generated name of the index.
+     *
+     * @return  $this
+     */
+    public function generatedName(string $name): static
+    {
+        $this->name($name);
+        $this->generatedName = true;
+
+        return $this;
+    }
+
+    /**
+     * Returns the index columns.
+     *
+     * @return  string[]
+     */
+    public function getColumns(): array
+    {
+        if (!isset($this->columns)) {
+            throw new \BadMethodCallException(
+                "Before getting the columns, they must be set for index '{$this->getName()}'."
+            );
+        }
+
+        return $this->columns;
+    }
+
+    /**
+     * Returns the data used by `DatabaseEditor` to add the index to a table.
+     *
+     * @return  array{columns: string, type: string}
+     */
+    public function getData(): array
+    {
+        return [
+            'columns' => \implode(',', $this->getColumns()),
+            'type' => $this->getType(),
+        ];
+    }
+
+    /**
+     * Returns the name of the index.
+     */
+    public function getName(): string
+    {
+        return $this->name;
+    }
+
+    /**
+     * Returns the type of the index (see `*_TYPE` constants).
+     */
+    public function getType(): ?string
+    {
+        return $this->type;
+    }
+
+    /**
+     * Returns `true` if the name of the index has been automatically generated.
+     */
+    public function hasGeneratedName(): bool
+    {
+        return $this->generatedName;
+    }
+
+    /**
+     * Sets the name of the index.
+     *
+     * @return  $this
+     */
+    public function name(string $name): static
+    {
+        $this->name = $name;
+
+        return $this;
+    }
+
+    /**
+     * Sets the type of the index and returns the index
+     *
+     * @throws  \InvalidArgumentException   if given type is invalid
+     */
+    public function type(?string $type): static
+    {
+        if (
+            $type !== static::DEFAULT_TYPE
+            && $type !== static::PRIMARY_TYPE
+            && $type !== static::UNIQUE_TYPE
+            && $type !== static::FULLTEXT_TYPE
+        ) {
+            throw new \InvalidArgumentException("Unknown index type '{$type}'.");
+        }
+
+        $this->type = $type;
+
+        return $this;
+    }
+
+    /**
+     * Returns a `DatabaseTableIndex` object with the given name.
+     */
+    public static function create(string $name): static
+    {
+        return new static($name);
+    }
+
+    /**
+     * Returns a `DatabaseTableIndex` object with the given name and data.
+     *
+     * @param array $data data returned by `DatabaseEditor::getIndexInformation()`
+     */
+    public static function createFromData(string $name, array $data): static
+    {
+        return self::create($name)
+            ->type($data['type'])
+            ->columns($data['columns']);
+    }
 }