PHPackages                             tomasvotruba/ctor - 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. tomasvotruba/ctor

ArchivedPhpstan-extension[Utility &amp; Helpers](/categories/utility)

tomasvotruba/ctor
=================

Prefer constructor over always called setters

2.2.5(3w ago)1524.9k↓51.2%114MITPHPPHP ^7.4|^8.0

Since Jul 9Pushed 2w ago1 watchersCompare

[ Source](https://github.com/TomasVotruba/ctor)[ Packagist](https://packagist.org/packages/tomasvotruba/ctor)[ Fund](https://www.paypal.me/rectorphp)[ GitHub Sponsors](https://github.com/tomasvotruba)[ RSS](/packages/tomasvotruba-ctor/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (1)Dependencies (16)Versions (18)Used By (14)

Ctor - Deprecated
=================

[](#ctor---deprecated)

[![Downloads](https://camo.githubusercontent.com/f7846b1e9fa97144c1e4c924cc2a5b1302f68251ffa22e5bcca31c970d1ab490/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746f6d6173766f74727562612f63746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tomasvotruba/ctor/stats)

**⚠️ This package is deprecated.** The rule has been merged into [symplify/phpstan-rules](https://github.com/symplify/phpstan-rules), use it instead:

```
composer require symplify/phpstan-rules --dev
```

Then enable the rule in your `phpstan.neon`:

```
parameters:
    ctor: true
```

---

If you can use constructor instead of setters, use it. These PHPStan rules will help you to find such cases.

What It Does
------------

[](#what-it-does)

This tool collects instances of `new object()` followed by a series of method calls on the same object:

```
$human = new Human();
$human->setName('Tomas');
$human->setAge(35);
```

...and suggests turning them into constructor arguments:

```
$human = new Human('Tomas', 35);

// named arguments work too, if the constructor grows wide
$human = new Human(name: 'Tomas', age: 35);
```

Both `set*` and `add*` method prefixes are treated as setters, so `$collection->addItem(...)` chains are flagged the same way.

### Why?

[](#why)

Such chained setters often indicate implicit required dependencies. By moving them to the constructor, you make the object state explicit, safer, and easier to reason about — and even easier to test.

Requirements
------------

[](#requirements)

- PHP `7.2`+
- PHPStan `^2.1`

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

[](#installation)

```
composer require tomasvotruba/ctor --dev
```

Usage
-----

[](#usage)

Use [`phpstan/extension-installer`](https://github.com/phpstan/extension-installer) to load the extension automatically. Run PHPStan and the rule will pick up.

Without the extension installer, include the config manually in your `phpstan.neon`:

```
includes:
    - vendor/tomasvotruba/ctor/config/extension.neon
```

What You'll See
---------------

[](#what-youll-see)

When the rule fires, PHPStan reports:

```
Class "App\Human" is always created with same 2 setter(s): "setAge()", "setName()"
Pass these values via constructor instead

```

The error identifier is `tv.newOverSetters` — use it to ignore specific cases via PHPStan's `ignoreErrors`.

When the Rule Fires (and When It Doesn't)
-----------------------------------------

[](#when-the-rule-fires-and-when-it-doesnt)

The rule is intentionally conservative. It only reports a class when:

- The same class is instantiated **at least twice** across the analysed code, **with the same set of setters each time**. A single `new + setters` block on its own is not enough — there must be a repeated pattern.

It deliberately **skips**:

- Doctrine entities (files containing `@ORM\Entity` or `#[Entity]`)
- Symfony `HttpKernel\Kernel` subclasses
- Vendor code
- `new + setters` blocks interrupted by a `return` or `throw` (likely conditional construction)

Happy coding!

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance96

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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 ~21 days

Recently: every ~41 days

Total

16

Last Release

24d ago

Major Versions

0.2.0 → 2.2.02025-12-12

0.2.1 → 2.2.12025-12-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/924196?v=4)[Tomas Votruba](/maintainers/TomasVotruba)[@TomasVotruba](https://github.com/TomasVotruba)

---

Top Contributors

[![TomasVotruba](https://avatars.githubusercontent.com/u/924196?v=4)](https://github.com/TomasVotruba "TomasVotruba (42 commits)")

---

Tags

constructorextensionphpstansolid

###  Code Quality

TestsPHPUnit

Static AnalysisRector

### Embed Badge

![Health badge](/badges/tomasvotruba-ctor/health.svg)

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

###  Alternatives

[symplify/monorepo-builder

Not only Composer tools to build a Monorepo.

5255.7M110](/packages/symplify-monorepo-builder)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136400.8k14](/packages/rector-rector-src)[ssch/typo3-rector

Instant fixes for your TYPO3 PHP code by using Rector.

2603.0M382](/packages/ssch-typo3-rector)[phpactor/phpactor

PHP refactoring and intellisense tool for text editors

1.9k16.6k1](/packages/phpactor-phpactor)[ticketswap/phpstan-error-formatter

A minimalistic error formatter for PHPStan

87670.5k53](/packages/ticketswap-phpstan-error-formatter)[sylius/promotion

Flexible promotion management for PHP applications.

28495.4k13](/packages/sylius-promotion)

PHPackages © 2026

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