SublimeLinter / SublimeLinter-php

SublimeLinter 3 plugin for PHP, using php -l.
MIT License
209 stars 29 forks source link

Not linting with php embedded on file with javascript syntax #21

Closed alexandre1985 closed 8 years ago

alexandre1985 commented 8 years ago

I use codeigniter in my work. That means that I have javascript code on .php files. So in Sublime Text 3, I put those .php files with a Javascript syntax. The problem is that when I put php embedded code on those files (that has a Javascript syntax set) I don't get any linting from the sublimelinter-php. What should I do? Here is a screenshot of my Sublime Text: a

ajmichels commented 8 years ago

What version of PHP are you using locally? php -v

Also are you running any other linters on this file or just SublimeLinter-php?

When I run your code through the linter directly I am not get any errors. php -l -n -d display_errors=On -d log_errors=Off view_gridMes_js.php I am also not able to reproduce what you are seeing in Sublime when I lint the file with SublimeLinter-php.

ajmichels commented 8 years ago

Ah. So you are actually linting this file as JavaScript not as PHP since you have the syntax set to JavaScript. I think you need to pose your question to whatever linter is actually causing these errors. However, I think you are going to find that they will not be able to help you either. That file will never successfully lint as JS because you have PHP in it. That said there are a few ways to get around this. Some of them are kind of hacky. First off you say you have to put your JS in a PHP file because you are using CodeIgniter... I am not so sure that is actually the case. I think you have made an architectural decision here to do so. I think you are far better off limiting your intersection of PHP and JS as much as possible just as limiting your intersection of PHP and HTML is also beneficial.

My first recommendation would be to define these PHP originated values in a different view and then reference them in this file.

<script>
var mesActual = <?php echo date('n'); ?>;
var mesesArray = <?php
        $mesesArray = array();
        $meses = array("Jenerio", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro",
            "Outubro", "Novembro", "Dezembro");
        $num_meses = count($meses);
        for($i=1; $i <= $num_meses; $i++) {
            $mesesArray[] = array('id' => (string) $i, 'name' => (string) $meses[$i-1]);
        }
        echo json_encode($mesesArray);
    ?>;
</script>
var storeMes = Ext.create('Ext.data.Store', {
    fields: ['id', 'name'],
    data : window.mesesArray
});

This way your JS files can just be JS files and will lint correctly. Your PHP files are still pretty simple and if you lint them as PHP files you shouldn't have any issues. Keep your JS variable definitions within PHP files simple and you won't have to worry so much about linting them as JS.

Another even hackier version is to tuck your PHP code away inside of strings.

var mesActual = '<?php echo date('n'); ?>';

The problem with this approach is that there are many values that you don't actually want as strings and you will then have to parse them within your JS code which means your JS is doing way more than it needs to and is way more complicated. It works in a pinch but I would recommend the first approach before this one.

The solution you currently have is not ideal for writing JS or PHP. You aren't getting good linting for your JS and you aren't getting any syntax help with your PHP.

alexandre1985 commented 8 years ago

Thank you for your response. I have tried your first solution but everything that appears after gets an odd color. Here's a screenshot bellow. captura de ecra de 2016-08-23 16 11 42 Is there anything else that I should do in order to get good linting on JS with PHP?

ajmichels commented 8 years ago

Sorry, I was wasn't clear. That <script> block would be placed in a different PHP view file. Then this file that you are currently looking at would not have any PHP code in it at all and would just be JavaScript. The idea is to keep your JS and PHP separate as much as possible. Because JS is a scripting language like PHP and not a markup language like XML or HTML it doesn't really work very well for templating. HTML is a better place for you to be mixing PHP and JS, which is why I am suggesting you define your dynamic JS variables in a file that is mostly HTML. It is hard for me to give a more specific example when I can't see the full code base you are working with. If I had more time I would throw together an example repo for you but unfortunately I don't. Hope this helps.

alexandre1985 commented 8 years ago

I have got it ;) Thank you. In work I'm not the only one working on this code, so I can't put it on a different file. They will think it's too confusing and will not approve it. I think I' gonna disable linting. Thank you anyway