susina/psr2-code-generator

View on GitHub
src/Model/AbstractPhpMember.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php declare(strict_types=1);
/*
 * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Susina\Codegen\Model;

use gossi\docblock\Docblock;
use Susina\Codegen\Model\Parts\DocblockPart;
use Susina\Codegen\Model\Parts\LongDescriptionPart;
use Susina\Codegen\Model\Parts\NamePart;
use Susina\Codegen\Model\Parts\TypePart;

/**
 * Abstract PHP member class.
 *
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 * @author Thomas Gossmann
 */
abstract class AbstractPhpMember extends AbstractModel implements DocblockInterface
{
    use DocblockPart;
    use LongDescriptionPart;
    use NamePart;
    use TypePart;

    /**
     * Private visibility.
     *
     * @var string
     */
    const VISIBILITY_PRIVATE = 'private';

    /**
     * Protected visibility.
     *
     * @var string
     */
    const VISIBILITY_PROTECTED = 'protected';

    /**
     * Public visibility.
     *
     * @var string
     */
    const VISIBILITY_PUBLIC = 'public';

    /** @var bool */
    private $static = false;

    /** @var string */
    private $visibility = self::VISIBILITY_PUBLIC;

    /** @var ?AbstractPhpStruct */
    private $parent;

    /**
     * Creates a new member.
     *
     * @param string $name the name of the member
     */
    public function __construct(string $name = '')
    {
        $this->setName($name);
        $this->docblock = new Docblock();
    }

    /**
     * Sets the members visibility.
     *
     * @see self::VISIBILITY_PUBLIC
     * @see self::VISIBILITY_PROTECTED
     * @see self::VISIBILITY_PRIVATE
     *
     * @param string $visibility the new visibility
     *
     * @return $this
     */
    public function setVisibility(string $visibility): self
    {
        if (self::VISIBILITY_PRIVATE !== $visibility
                && self::VISIBILITY_PROTECTED !== $visibility
                && self::VISIBILITY_PUBLIC !== $visibility) {
            throw new \InvalidArgumentException(sprintf('The visibility "%s" does not exist.', $visibility));
        }

        $this->visibility = $visibility;

        return $this;
    }

    /**
     * Returns the visibility state of this member.
     *
     * @return string the visibility
     */
    public function getVisibility(): string
    {
        return $this->visibility;
    }

    /**
     * Sets whether or not this member is static.
     *
     * @return $this
     */
    public function setStatic(bool $bool): self
    {
        $this->static = (bool) $bool;

        return $this;
    }

    /**
     * Returns whether this member is static.
     *
     * @return bool `true` if static and `false` if not
     */
    public function isStatic(): bool
    {
        return $this->static;
    }

    /**
     * Sets the parent structure to which this member belongs.
     *
     * @internal
     *
     * @return $this
     */
    public function setParent(?AbstractPhpStruct $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Returns the parent structure to which this member belongs.
     *
     * @internal
     *
     * @return AbstractPhpStruct
     */
    public function getParent(): ?AbstractPhpStruct
    {
        return $this->parent;
    }
}