# Make phpstan work with ctp files

**URL:** <https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396>\
**Category:** Need Help\
**Created:** [December 10, 2018, 4:35pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396 "2018-12-10T16:35:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [December 10, 2018, 4:35pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/1 "2018-12-10T16:35:58Z")

</div>

We are using [`phpstan`](https://github.com/phpstan/phpstan) to detect and fix some common errors. This tool works fine with plain PHP files or classes, but not with `ctp` files. This is our `phpstan.neon` config file:

```auto
parameters:
    fileExtensions:
      - php
      - ctp

```

But if we analyze a `ctp` file, we get errors like this:

```auto
$ phpstan analyze --level 0 src/Template/xxx/info.ctp
Note: Using configuration file xxx/phpstan.neon.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ -------------------------------
  Line
 ------ -------------------------------
  7 Undefined variable: $this
  7 Using $this outside a class.
  42 Undefined variable: $authUser
  47 Undefined variable: $authUser
  52 Undefined variable: $authUser
  57 Undefined variable: $authUser
 ------ -------------------------------

 [ERROR] Found 6 errors

```

Does anyone know a configuration to make this work?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 11, 2018, 1:44am UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/2 "2018-12-11T01:44:16Z")

</div>

Looks like phpstan supports docblocks, so use something like this at the top of your ctp files:

```
/**
 * @var View $this
 * @var string $authUser
 */

```

It’s a lot of work to add them all, but you also get the added bonus of autocomplete when editing the files, if your editor supports that.

---

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [December 11, 2018, 4:55pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/3 "2018-12-11T16:55:14Z")

</div>

After some more research, it looks that phpstan doesn’t work well with that kind of annotations: [https://github.com/phpstan/phpstan/issues/351](https://github.com/phpstan/phpstan/issues/351). Anayway, thanks for the response; it is useful for editors.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 11, 2018, 6:49pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/4 "2018-12-11T18:49:13Z")

</div>

That’s a shame. Worse, it looks like the author does not see any value in adding that support.

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [December 12, 2018, 8:54am UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/5 "2018-12-12T08:54:45Z")

</div>

PHPStan works quite well for us, and for CakePHP core development. You can use [https://github.com/dereuromark/cakephp-ide-helper](https://github.com/dereuromark/cakephp-ide-helper) to make the changes automatically for you.

---

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [December 12, 2018, 9:17am UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/6 "2018-12-12T09:17:09Z")

</div>

Hi @dakota, do you also validate `ctp` files? The problem is with that kind of files and the variable `$this`.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 12, 2018, 11:50am UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/7 "2018-12-12T11:50:21Z")

</div>

I wouldn’t suggest trying to use phpstan for .ctp files. The template files are basically php code fragments which are included inside View class. So by themselves they don’t have enough context available for phpstan to do it’s job. You’ll have to go about adding annotations like `/** @var FooType $bar */` throughout the template files.

---

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [December 12, 2018, 12:24pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/8 "2018-12-12T12:24:33Z")

</div>

Yes, I know this limitation of phpstan… Does anyone use any static analyzer for ctp files that respect that kind of annotations? I am reviewing the alternatives mentioned here [https://www.reddit.com/r/PHP/comments/5uy9fw/php\_static\_analysis\_tool\_discover\_bugs\_in\_your/](https://www.reddit.com/r/PHP/comments/5uy9fw/php_static_analysis_tool_discover_bugs_in_your/) but I would like to know your opinions.

---

<div class="post-metadata">

**Author:** ![ADmad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/admad/32/20_2.png) [@ADmad](https://discourse.cakephp.org/u/ADmad)\
**Post date:** [December 15, 2018, 12:54pm UTC](https://discourse.cakephp.org/t/make-phpstan-work-with-ctp-files/5396/9 "2018-12-15T12:54:18Z")

</div>

phpstan supports annotations just fine.
