phpshots/common-type-alias

A common library for PHPShots applications providing type alias management functionality.

v0.1.1 2024-10-27 11:35 UTC

This package is auto-updated.

Last update: 2024-10-27 11:43:06 UTC


README

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

PHPShots/Common [Type Alias Manager]

A common library for managing type aliases in PHP.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Roadmap
  4. Contributing
  5. License
  6. Contact
  7. Acknowledgments

About The Project

This project is a PHP library designed to create and manage type aliases, making code more readable and maintainable. Type aliases can simplify complex type definitions and enhance the developer experience.

Why Use Type Alias?

  • Improves code clarity and reduces redundancy.
  • Makes collaboration easier by standardizing types.
  • Enhances type safety in your applications.

Feel free to explore and contribute!

Built With

Getting Started

Follow these steps to set up and start using the Type Alias library in your PHP project.

Prerequisites

Before you begin, ensure you have the following installed:

  • PHP (version 8.2 or higher)
    apt install php
  • Composer (for dependency management)
      apt install composer

Installation

  1. Clone the Repository: Start by cloning the repository to your local machine:

    git clone https://github.com/hakrichTech/type-alias.git
  2. Navigate to the Project Directory

    cd type-alias
  3. Install Dependencies: Use Composer to install the required dependencies:

    composer install

Basic Setup

  1. Include the Library: In your PHP script, include the Composer autoload file to access the Type Alias library:

    require 'vendor/autoload.php'; // Adjust the path as necessary
  2. Using the Type Alias Library: Start using the library by creating type aliases. Here’s a basic example:

    use PHPShots\Common\TypeAlias;
    
    // Create an instance of TypeAlias
    $typeAlias = new TypeAlias;
    
    // Create a type alias
    $typeAlias->alias('MyAlias', 'OriginalType');
    
    // Example of an original type
    class OriginalType {
        public function sayHello(string $type) {
            return "Hello from $type!";
        }
    }
    
    // Instantiate using the alias
    $myVariable = new MyAlias();
    echo $myVariable->sayHello($typeAlias->getAlias('MyAlias')); // Outputs: Hello from OriginalType!
  • isAlias: Checks if a name is already an alias.

      // Check if a name is an alias
      if ($typeAlias->isAlias('MyAlias')) {
          echo "MyAlias is an OriginalType.\n";
      }
  • removeAbstractAlias: Removes a specific alias from the abstract and aliases list.

    // Remove an alias
    $typeAlias->removeAbstractAlias('MyAlias');
  • getAlias: Gets the ultimate alias for an abstract type, following any alias chain.

    // Retrieve the ultimate alias
    echo "Ultimate alias for MyAlias: " . $typeAlias->getAlias('MyAlias') . "\n";
  • alias: Adds an alias for an abstract type, with checks to prevent self-aliasing.

    // Register some aliases
    $typeAlias->alias('SomeClass', 'Alias1');
    $typeAlias->alias('Alias1', 'Alias2'); // chaining alias
  • aliasReverse: Provides an alternative alias method with reversed parameter order.

    // Register some aliases in reversed order
    $typeAlias->alias('Alias1', 'SomeClass');
    $typeAlias->alias('Alias2', 'Alias1'); // chaining alias
  • getAllAliases: Returns all aliases for a given abstract.

    // List all aliases for a type
    print_r($typeAlias->getAllAliases('SomeClass'));
  • clearAliases: Clears all aliases at once.

    // Clear all aliases
    $typeAlias->clearAliases();
  • getAliasMap: Returns the entire alias map for quick inspection.

    // Display all direct mappings
    print_r($typeAlias->getAliasMap());  
  • hasAbstract: Checks if an abstract type has any associated aliases.

    // Check for aliases of an abstract type
    echo $typeAlias->hasAbstract('SomeClass') ? "SomeClass has aliases.\n" : "No aliases for SomeClass.\n";

Roadmap

  • Add Changelog
  • Add additional features and methods
  • Improve documentation
  • Implement unit tests

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Shamavu Rasheed - @hakeem-shamavu - shamavurasheed@gmail.com

Project Link: https://github.com/hakrichTech/type-alias

Acknowledgments

Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!

(back to top)