PHPackages                             eloquent/cosmos - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. eloquent/cosmos

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

eloquent/cosmos
===============

A library for representing and manipulating PHP class names.

2.3.1(13y ago)136.3k1[1 issues](https://github.com/eloquent/cosmos/issues)[1 PRs](https://github.com/eloquent/cosmos/pulls)2MITPHPPHP &gt;=5.3.0

Since Aug 4Pushed 10y ago1 watchersCompare

[ Source](https://github.com/eloquent/cosmos)[ Packagist](https://packagist.org/packages/eloquent/cosmos)[ Docs](https://github.com/eloquent/cosmos)[ RSS](/packages/eloquent-cosmos/feed)WikiDiscussions develop Synced 5d ago

READMEChangelog (7)Dependencies (3)Versions (11)Used By (2)

Cosmos
======

[](#cosmos)

*A library for representing and manipulating PHP class names.*

[![Build Status](https://camo.githubusercontent.com/4514d509ecd89ac51804ed30cbb03bcec228509c7b4a3d6b9901a6117ca11f1a/68747470733a2f2f7261772e6769746875622e636f6d2f656c6f7175656e742f636f736d6f732f67682d70616765732f6172746966616374732f696d616765732f696365636176652f726567756c61722f6275696c642d7374617475732e706e67)](http://travis-ci.org/eloquent/cosmos)[![Test Coverage](https://camo.githubusercontent.com/0e316911abfd94ac38160fda4a6297e1983811811d1ffe5bed741db962132428/68747470733a2f2f7261772e6769746875622e636f6d2f656c6f7175656e742f636f736d6f732f67682d70616765732f6172746966616374732f696d616765732f696365636176652f726567756c61722f636f7665726167652e706e67)](http://eloquent-software.com/cosmos/artifacts/tests/coverage/)

Installation
------------

[](#installation)

Available as [Composer](http://getcomposer.org/) package [eloquent/cosmos](https://packagist.org/packages/eloquent/cosmos).

What is Cosmos?
---------------

[](#what-is-cosmos)

Cosmos is a library for representing and manipulating PHP class names. It includes a `ClassName` object, as well as tools to help in resolving class names against a set of `namespace` and `use` statements.

ClassName object
----------------

[](#classname-object)

This object is designed to represent any valid PHP class or namespace name. There are two ways to create a new `ClassName` object. From a string or from an array representing the parts of the class name, or 'atoms'. `ClassName` objects cannot be instatiated directly.

The two methods are demostrated below:

```
use Eloquent\Cosmos\ClassName;

$earth = ClassName::fromString('\MilkyWay\SolarSystem\Earth');
$mars = ClassName::fromAtoms(array('MilkyWay', 'SolarSystem', 'Mars'), true);
```

Note the second parameter in the `fromAtoms()` method. This boolean value determines whether the class name is absolute (starts with a namespace separator).

From here the `ClassName` object can be used to manipulate the class name or extract information about its constituent parts.

### ClassName::atoms()

[](#classnameatoms)

Returns the parts of the class name as an array of strings.

### ClassName::isAbsolute()

[](#classnameisabsolute)

Returns boolean true if the class name is absolute (begins with a namespace separator) or boolean false if it does not.

### ClassName::isShortName()

[](#classnameisshortname)

Returns boolean true if the class name is not absolute and has only one atom. If either of these conditions is not met, false is returned.

### ClassName::isEqualTo(ClassName $className)

[](#classnameisequaltoclassname-classname)

Returns boolean true if the supplied class name is exactly equal to this class name. The supplied class name must have identical atoms and match the absolute-ness of this class name.

### ClassName::isRuntimeEquivalentTo(ClassName $className)

[](#classnameisruntimeequivalenttoclassname-classname)

Returns boolean true if the supplied class name is equivalent to this class name in a runtime context. The supplied class name must have identical atoms but absolute and relative class names are treated identically by PHP at runtime.

### ClassName::join(ClassName $className)

[](#classnamejoinclassname-classname)

Appends `$className` to the end of this class name and returns the result as a new `ClassName` object. Note that absolute class names cannot be joined.

### ClassName::joinAtoms($atom, ...)

[](#classnamejoinatomsatom-)

Appends the supplied atoms to the end of this class name and returns the result as a new `ClassName` object.

### ClassName::joinAtomsArray(array $atoms)

[](#classnamejoinatomsarrayarray-atoms)

Appends the supplied array of atoms to the end of this class name and returns the result as a new `ClassName` object.

### ClassName::hasParent()

[](#classnamehasparent)

Returns boolean true if the class name has a parent namespace, or boolean false if it does not.

### ClassName::parent()

[](#classnameparent)

Returns the parent namespace of this class name as a new `ClassName` object.

### ClassName::shortName()

[](#classnameshortname)

Returns the last atom of this class name as a new `ClassName` object.

### ClassName::toAbsolute()

[](#classnametoabsolute)

Returns the absolute version of this class name as a `ClassName` object. If this class name is already absolute, it will simply return itself.

### ClassName::toRelative()

[](#classnametorelative)

Returns the relative version of this class name as a `ClassName` object. If this class name is already relative, it will simply return itself.

### ClassName::hasDescendant(ClassName $className)

[](#classnamehasdescendantclassname-classname)

Returns boolean true if this class name is one of the parent namespaces of `$className` or boolean false if it is not.

### ClassName::stripNamespace(ClassName $namespaceName)

[](#classnamestripnamespaceclassname-namespacename)

Strips `$namespaceName` from this class name and returns the result as a new, `ClassName` object relative to the supplied namespace name.

### ClassName::exists($useAutoload = true)

[](#classnameexistsuseautoload--true)

Returns boolean true if the class name exists. Note that this does not take into account if the class name is absolute or relative. The `$useAutoload` parameter can be specified to prevent autoloading if necessary.

### ClassName::string()

[](#classnamestring)

Returns a string representation of this class name. `ClassName` also implements `__toString()` which simply returns the result of this method.

Class name resolver
-------------------

[](#class-name-resolver)

To use the class name resolver, first create a new resolver to represent the set of `namespace` and `use` statements to resolve against.

The first parameter represents the `namespace` statement. It must be supplied as a fully-qualified `ClassName` object. The second parameter is an array of tuples representing the `use` statements.

If a 1-tuple is supplied, it represents a `use` statement without an `as`clause. A 2-tuple represents a use statement with an attached `as` clause. The first element of the tuple is always a fully-qualified `ClassName` object. If present, the second element must be a short `ClassName` object. That is, one without any namespace separators.

```
use Eloquent\Cosmos\ClassName;
use Eloquent\Cosmos\ClassNameResolver;

$resolver = new ClassNameResolver(
    ClassName::fromString('\MilkyWay\SolarSystem'), // namespace
    array(
        array(
            ClassName::fromString('\MilkyWay\AlphaCentauri\ProximaCentauri'), // use
        ),
        array(
            ClassName::fromString('\Andromeda\GalacticCenter'), // use
            ClassName::fromString('Andromeda'), // as
        ),
    )
);
```

The above resolver is analogous to the following PHP code:

```
namespace MilkyWay\SolarSystem;

use MilkyWay\AlphaCentauri\ProximaCentauri;
use Andromeda\GalacticCenter as Andromeda;
```

The created resolver can now be used to determine the canonical version of any class name. Note that in the example below, `ClassName` objects are returned, not plain strings.

```
echo $resolver->resolve(ClassName::fromString('Earth'));
// outputs '\MilkyWay\SolarSystem\Earth'

echo $resolver->resolve(ClassName::fromString('ProximaCentauri'));
// outputs '\MilkyWay\AlphaCentauri\ProximaCentauri'

echo $resolver->resolve(ClassName::fromString('Andromeda'));
// outputs '\Andromeda\GalacticCenter'

echo $resolver->resolve(ClassName::fromString('TNO\Pluto'));
// outputs '\MilkyWay\SolarSystem\TNO\Pluto'

echo $resolver->resolve(ClassName::fromString('\Betelgeuse'));
// outputs '\Betelgeuse'
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~35 days

Recently: every ~12 days

Total

7

Last Release

4821d ago

Major Versions

1.0.1 → 2.0.02013-01-14

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/100152?v=4)[Erin](/maintainers/ezzatron)[@ezzatron](https://github.com/ezzatron)

---

Top Contributors

[![ezzatron](https://avatars.githubusercontent.com/u/100152?v=4)](https://github.com/ezzatron "ezzatron (16 commits)")

---

Tags

phpclassnamespaceresolvernamesolverclassname

### Embed Badge

![Health badge](/badges/eloquent-cosmos/health.svg)

```
[![Health](https://phpackages.com/badges/eloquent-cosmos/health.svg)](https://phpackages.com/packages/eloquent-cosmos)
```

###  Alternatives

[yzen.dev/plain-to-class

Class-transformer to transform your dataset into a structured object

16293.9k6](/packages/yzendev-plain-to-class)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
